RDL Report not rendering - c#

I have this error when I generate the report.
Where can I set the connection of my rdl file?
AspNetCore.Reporting.LocalProcessingException: An error occurred during local report processing.;An error has occurred during report processing.
Query execution failed for dataset 'DataSet1'.
Login failed for user ''.
---> AspNetCore.ReportingServices.ReportProcessing.ProcessingAbortedException: An error has occurred during report processing.
Here's my code.
const int extension = 1;
var path = Path.Combine(_webHostEnvironment.WebRootPath, "Reports", "test.rdl");
var parameters = new Dictionary<string, string> { { "rp1", "Test Value to parameter" } };
var localReport = new LocalReport(path);
var result = localReport.Execute(RenderType.Pdf, extension, parameters);
return File(result.MainStream, "application/pdf");```
[1]: https://i.stack.imgur.com/XkwzU.jpg

Change your code like below:-
private readonly IHostingEnvironment _webHostEnvironment;
public HomeController( IHostingEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
}
public async Task<IActionResult> Print()
{
string mimetype = "";
int extension = 1;
var path = $"{this._webHostEnvironment.WebRootPath}\\Reports\\test.rdlc";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("rp1", "Test Value to parameter");
LocalReport localReport = new LocalReport(path);
var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimetype);
return File(result.MainStream, "application/pdf");
}
Also, don't forget to install some NuGet packages as needed.
Hope it will resolve your issue. for more details and connected things, you can read this article:-
http://blog.geveo.com/IntegratingRDLCReportsToNetCoreProjects
UPDATE
App.config
<connectionStrings>
<add
name="MyConnectionString"
connectionString="Data Source=DESKTOP-N41V6ER\SQLEXPRESS;Initial
Catalog=MyDatabase;User ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
User ID and Password means using SQL credentials, not Windows, but still very simple - just go into your Security section of your SQL Server and create a new Login. Give it a username and password, and give it rights to your database.
AGAIN UPDATE(Data Source Connect)
Now select the object and click next:-
below, the design of a simple report that allows displaying the (assume)product data in your connected database. then click finish.
then click the ok button:-
then you will find your database table in dataset:-
now you can drag and drop your data, which place you want to show in your report file.
.................................................................
.................................................................
AGAIN UPDATE(Data base Connect)
First right click on your report project- => Add=>New Item then you will show:-
above add an entity data model to the windows form project.then click Add.then:-
Above,click New Connection.
Above,click Continue.
Above, give your server name. my machine has just one (SQL server)installed for that i am using . and select SQL Server in Authentication box.
Above,then click Ok.you have obisouly select DB name which you want.
Above, type your DB name carefully which you already selected.
Above, I select my product table because I will show that. then click finish

Related

'Link Table Manager' functionality Programatically using c#

I want to write some custom code to perform the 'Link Table Manager' operation programatically. I have MS-Access database which is currently referencing external data from Excel,MS-Access and SQL server.
Before executing a Macro on my MS-Access database, I want to re-link the external data sources or provide new location of the data-source in case there is change in the location of the data source.
I have a table in MS-Access database which is Linked to an external SQL data source(Test database). Now I want it to be linked to Production Database. I have tried the following code but it is throwing a COM-Exception with error description as 'ODBC--connection to 'EmployeeConnectionForSQL' failed.'
public void performLinkTableMangerOperationForSQL()
{
string CurrentDatabasePath = #"D:\UDTDatabase\InternalDatabase.accdb";
Microsoft.Office.Interop.Access.Dao.DBEngine DAO = new Microsoft.Office.Interop.Access.Dao.DBEngine();
Microsoft.Office.Interop.Access.Dao.Database db;
Microsoft.Office.Interop.Access.Dao.TableDefs dt;
db = DAO.OpenDatabase(CurrentDatabasePath);
dt = db.TableDefs;
// Refreshing link for Sql server external table with DSN.
string sqlSource = #"DATABASE=Employee;";
string DSNName = "EmployeeConnectionForSQL;";
string sqlNewConnectionString = #"ODBC;FileDSN=" + DSNName + sqlSource;
foreach (Microsoft.Office.Interop.Access.Dao.TableDef table in dt)
{
string name = table.Name;
if (table.Name == "dbo_Employees")
{
table.Connect = sqlNewConnectionString;
table.RefreshLink();
}
}
db.Close();
}
That FileDSN is clearly invalid. A FileDSN should point to a .dsn file. If you want to use a normal DSN, use DSN=, not FileDSN=. Since there's an invalid DSN, you can't relink.
I highly recommend you go DSNless, and just use a connection string, but providing a valid FileDSN should work too.

MVC inject DataAccessLayer based on the logged in user

I've an application which insert/save data in different databases hosted on different server. UI may be different but at the end data which is getting saved is almost same.
So i want to use the same DataAccessLayer but want to change the connectionString based on the loggedin user.
Dependency can be configured in startup.cs but at that time i may not know the DataBase user would like to work with.
on login page i'm asking user to select the database to work with, so only way to change the connection string is after login page.
Any suggestion?
public class ConnectionRepository : IConnectionRepository
{
private IDbConnection _cnn = null;
public IDbConnection GetOpenConnection(string databaseName)
{
if (_cnn != null && _cnn.ConnectionString.ToLower().Contains(databaseName.ToLower()))
{
_cnn.Open();
return _cnn;
}
var cnn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
//Now replace database name in connection string with whichever one supplied
var cb = new SqlConnectionStringBuilder(cnn) { InitialCatalog = databaseName };
// wrap the connection with a profiling connection that tracks timings
return new ProfiledDbConnection(new SqlConnection(cb.ConnectionString), MiniProfiler.Current);
}
}
This code replaces the InitialCatalog (database name) part of the connection string dynamically base on the supplied name. The current name is stored in session when the user logs in.
Hope this helps.

Using Hangfire, connection string given in Startup.cs throws Cannot attach file as database error

I'm utilizing Hangfire in my ASP .Net MVC Web App, it had installed successfully. I'd like to use the same LocalDb to store queued jobs for Hangfire to dequeue and process as I've used to stored data. However I'm running into the below error when I provided its connectionString or name defined in Web.config in Startp.cs. I've had no trouble adding, deleting updating data in the same localDb before hangfire.
Cannot attach the file 'c:\users\jerry_dev\documents\visual studio 2013\Projects\Hangfire.Highlighter\Hangfire.Highlighter\App_Data\aspnet-Hangfire.Highlighter-20150113085546.mdf' as database 'aspnet-Hangfire.Highlighter-20150113085546'.
Startup.cs:
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.UseHangfire(config =>
{
string hangfireConnectionString = #"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Hangfire.Highlighter-20150113085546.mdf;Initial Catalog=aspnet-Hangfire.Highlighter-20150113085546;Integrated Security=True";
config.UseSqlServerStorage(hangfireConnectionString);
config.UseServer();
});
}
My project Solution is named "Hangfire.Highlighter"
Web.config:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Hangfire.Highlighter-20150113085546.mdf;Initial Catalog=aspnet-Hangfire.Highlighter-20150113085546;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
I know this is old - but its been 9 months and I pulled my hair out over this as well - and decided to do a write-up on it here.
My solution was to just create a quick and dirty DbContext, point it to the proper connection string, and call Database.CreateIfNotExists in the constructor:
public class HangfireContext : DbContext
{
public HangfireContext() : base("name=HangfireContext")
{
Database.SetInitializer<HangfireContext>(null);
Database.CreateIfNotExists();
}
}
In the HangfireBootstrapper.Start() method I do something like this:
public void Start()
{
lock (_lockObject)
{
if (_started) return;
_started = true;
HostingEnvironment.RegisterObject(this);
//This will create the DB if it doesn't exist
var db = new HangfireContext();
GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireContext");
// See the next section on why we set the ServerName
var options = new BackgroundJobServerOptions()
{
ServerName = ConfigurationManager.AppSettings["HangfireServerName"]
};
_backgroundJobServer = new BackgroundJobServer(options);
var jobStarter = DependencyResolver.Current.GetService<JobBootstrapper>();
//See the Recurring Jobs + SimpleInjector section
jobStarter.Bootstrap();
}
}
Not sure why Hangfire has such a hard time with LocalDb - maybe it can only handle full-blown SQL instances? Either way this works for me, new team members, and new dev/staging/prod instances that get stood up.
I too know this is old, but ran into this recently. Here is my fix:
In Visual Studio, go to 'View -> SQL Server Object Explorer'
Connect to the Data Source if it isn't already connected. In the example above it was '(LocalDb)\v11.0'
Right click on 'Databases' -> 'Add New Database'
Fill Database name = Ex: 'aspnet-Hangfire.Highlighter-20150113085546' or whatever you've named the database in the connection string.
Fill Database location = This should be the Data Directory in your application, 'App_Data' for the MVC project.
This fixed the issue in my case.
Jack's answer didn't work for me, because I ran into this problem: No connection string named could be found in the application config file
I got it to work with the following modifications:
Remove "name=" from the string in the base initializer. Thanks to: https://stackoverflow.com/a/37697318/2279059
This moves the error to the call of UseSqlServerStorage. So instead of passing "HangfireContext" to it, I just copy the connection string from the dummy database context.
Complete setup code:
public class HangfireContext : DbContext
{
public HangfireContext() : base("HangfireContext") // Remove "name="
{
Database.SetInitializer<HangfireContext>(null);
Database.CreateIfNotExists();
}
}
public partial class Startup
{
public static void ConfigureHangfire(IAppBuilder app)
{
var db = new HangfireContext();
GlobalConfiguration.Configuration.UseSqlServerStorage(db.Database.Connection.ConnectionString); // Copy connection string
app.UseHangfireDashboard();
app.UseHangfireServer();
}
}
Is the DB already created? Can you try using a different conneciton string format?something like this,
"Server=.;Database=HangFire.Highlighter;Trusted_Connection=True;"
Answer as per AspNetCore 3.1 and Hangfire 1.7.17
Hangfire should create all the tables provided there is an existing database with the specified database name.
If you want to use LocalDb, you can use the following registrations (see below).
services
.AddHangfire(
(serviceProvider, config) =>
{
//read settings or hardcode connection string, but this is cleaner
var configuration = serviceProvider.GetService<IConfiguration>();
var connectionString = configuration.GetValue<string>("Hangfire:ConnectionString");
var sqlServerStorageOptions =
new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
};
config
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings();
.UseSqlServerStorage(connectionString, sqlServerStorageOptions);
});
The connection string is read, in my example, from the appsettings, so it'd look like this
"Hangfire": {
"ConnectionString": "Data Source=(localdb)\\MsSqlLocalDb; Database=Hangfire;"
}
Again, notice how the connection string has the name of the database (e.g: Hangfire) that MUST exist in localdb. If you remove the Database=xxx parameter altogether, it'll pick the master database by default and create all the tables there.

Entity Framework throws exception - Network Related or instance-specific error occurred

I've followed a tutorial in MVC 3, and done this:
Made 4 classes and a DBClass:
User [has a] List of Websites [has a] List of Pages [has a] List of Comments [each marked word is a class of its own].
Then, I made a class called UserDataBaseDB this way:
public partial class UserDataBaseDB : DbContext
{
public DbSet<User> Users { get; set; }
public UserDataBaseDB()
: base(#"UserDataBaseDB")
{
if (this.Database.Exists())
{
try
{
this.Database.CompatibleWithModel(false);
}
catch (InvalidOperationException)
{
this.Database.Delete();
}
}
if (!this.Database.Exists())
{
this.Database.Create();
Seed();
}
}
private void Seed()
{
var Mark = new User();
Mark.ID = 0;
Mark.MD5Pass = "4297f44b13955235245b2497399d7a93";
Mark.UserName = "Admin";
var Troll = new Comment();
Troll.CommentData = "Fake!";
Troll.Writer = Mark;
var Site = new Website();
Site.Name = "Admin Site";
Site.Pages.ElementAt(0).Data = "Awesome website!";
Site.Pages.ElementAt(0).Comments.Add(Troll);
Mark.Websites.Add(Site);
Users.Add(Mark);
}
public static UserDataBaseDB Create()
{
return new UserDataBaseDB();
}
public User FindUserByID(int id)
{
return (from item in this.Users
where item.ID == id
select item).SingleOrDefault();
}
Then I added the Controller and View, and navigated to http://localhost:40636/Main, and I saw this message:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Line 14: : base(#"UserDataBaseDB")
Line 15: {
Line 16: **if (this.Database.Exists())**
Line 17: {
Line 18: try
Letting the marked line be the red line.
I've tried to re-install Entity Framework, also trying EFCodeFirst, tried making my own SqlDataBase and giving it the connection string, but nothing worked.
Does anyone know what's the solution? Thank you!
PS: I'm using VS Ultimate SP1 on a Win7 64bit machine.
This is likely a connect string issue, however I do see that you are trying to seed information and fully use the context in the constructor, which I would highly recommend you dont do.
You can implement the OnModelCreating method and attempt your initialization code there, or even better in your application start. It just doesnt seem right to be stuck in the constructor as initialization is just starting at this point.
Set a breakpoint on
if (this.Database.Exists())
Does the error happen immediately after that line? If so, triple check your connect string. Do you have your code in one project, or broken out? Make sure you are checking the connect string in your root web project's web.config and not any component's app.config if you so happen to be using that design.
Each time you changes from code-first, etc did you change your connect string? A code-first connect string is named after your context class and is simple:
<add name="UserDataBaseDB" connectionString="Data Source=|DataDirectory|test.sdf" providerName="System.Data.SqlServerCe.4.0" /%gt;
That is of course using SQLCe4. Sql Server would be
<add name="UserDataBaseDB" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=YourDbName;Integrated Security=True;Pooling=False" /%gt;
or for a db in app_Data
<add name="UserDataBaseDB" providerName="System.Data.SqlClient" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\YourDatabase.mdf;User Instance=true" /%gt;

Best practice to create (on demand) SQL Server 2008 Express databases in C#?

The purpose is to handle the user's data (you can call them project, document, file, or whatever) in a brand new SQL Server 2008 Express database. The data are expected to occupy much less space than the 4GB available with the express edition (which is also free to distribute).
E.g., each time the user selects File->New command, a new empty database will be created at the specified location. On the other hand, a similar command, File->Open must provide support to retrieve the list of the databases to select one for opening.
So, the following issues must be resolved:
a) The application must be able to create the connection string and attach the database to SQL Server 2008 Express through code (C#)
b) The application must be able to retrieve (again through code) a list with all the available databases, to give the user a chance to select one to open.
I think it would be helpful to have a template database in resources and copy it in the location specified by the user.
Do you think it is a working solution? Do you have any suggestions?
There's lots you can do with Sql Server Management Objects (SMO):
// Add a reference to Microsoft.SqlServer.Smo
// Add a reference to Microsoft.SqlServer.ConnectionInfo
// Add a reference to Microsoft.SqlServer.SqlEnum
using Microsoft.SqlServer.Management.Smo;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
public class SqlServerController
{
private Server m_server = null;
public SqlServerController(string server)
{
m_server = new Server(server);
}
public void AttachDatabase(string database, StringCollection files,
AttachOptions options)
{
m_server.AttachDatabase(database, files, options);
}
public void AddBackupDevice(string name)
{
BackupDevice device = new BackupDevice(m_server, name);
m_server.BackupDevices.Add(device);
}
public string GetServerVersion(string serverName)
{
return m_server.PingSqlServerVersion(serverName).ToString();
}
public int CountActiveConnections(string database)
{
return m_server.GetActiveDBConnectionCount(database);
}
public void DeleteDatabase(string database)
{
m_server.KillDatabase(database);
}
public void DetachDatabase(string database, bool updateStatistics,
bool removeFullTextIndex)
{
m_server.DetachDatabase(database, updateStatistics, removeFullTextIndex);
}
public void CreateDatabase(string database)
{
Database db = new Database(m_server, database);
db.Create();
}
public void CreateTable(string database, string table,
List<Column> columnList, List<Index> indexList)
{
Database db = m_server.Databases[database];
Table newTable = new Table(db, table);
foreach (Column column in columnList)
newTable.Columns.Add(column);
if (indexList != null)
{
foreach (Index index in indexList)
newTable.Indexes.Add(index);
}
newTable.Create();
}
public Column CreateColumn(string name, DataType type, string #default,
bool isIdentity, bool nullable)
{
Column column = new Column();
column.DataType = type;
column.Default = #default;
column.Identity = isIdentity;
column.Nullable = nullable;
return column;
}
public Index CreateIndex(string name, bool isClustered, IndexKeyType type,
string[] columnNameList)
{
Index index = new Index();
index.Name = name;
index.IndexKeyType = type;
index.IsClustered = isClustered;
foreach (string columnName in columnNameList)
index.IndexedColumns.Add(new IndexedColumn(index, columnName));
return index;
}
}
An alternate solution is to use SQLite rather than SQL Express. You can even continue to use ADO.NET if you use this solution. SQLite databases are simply files, and your connection strings can refer to the file path. When a user wants to open their file, they can select an actual file.
I get the impression that this database will live locally on user's machine. If that's the case, sql server express is not usually a good database choice. It's a server-class engine rather than a desktop or in process engine. Instead, there are a number of good in process engines you can use: Sql Server Compact Edition, Sqlite (as mentioned by Jacob) or even Access.
If you believe SQL Server Express 2008 is the right choice (sqllite does seem to fit better though), I would look at using User Instances which will allow non-administrators to add databases from files as you describe.
This article shows how to create a new database, and attach it to a SQL Server database instance:
How to: Attach a Database File to SQL Server Express
http://msdn.microsoft.com/en-us/library/ms165673.aspx
These article shows how to manage the attaching and detaching of existing databases:
http://msdn.microsoft.com/en-us/library/ms190794.aspx
http://www.databasejournal.com/features/mssql/article.php/2224361/Attaching-and-Detaching-Databases-on-SQL-Server.htm
For the following connection string for SQL Server 2008 R2.
<connectionstring>Data Source=.\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=True</connectionstring>
you can do
var connectionString = new SqlConnectionStringBuilder(connectionString);
var serverConnection = new ServerConnection("DatabaseInstanceName in server");
var serverInstance = new Server(serverConnection);
if (serverInstance.Databases.Contains(connectionString.InitialCatalog))
serverInstance.KillDatabase(connectionString.InitialCatalog);
var db = new Database(serverInstance, connectionString.InitialCatalog);
try
{
db.Create();
}
catch (SqlException ex)
{
throw;
}
Thanks to Mr. Harvey for pointing the right direction. Although in my case, I have to make these small changes. Because, I use the windows authentication.

Categories