Set Dynamic Database path for crystalreport
-
Monday, March 03, 2008 11:57 AM
I am using Dot net 2.0 using web application.
While creating report i have given path for crystalreport like.
reportDocument.SetDatabaseLogon("Samvaad", "Samvaad", "m3", "Samvaad", false);It is ok as far as i am using same database and server.
But now i want to set database path dynamically.
How i set path dynamically.
Please anyone give me the procedure of setting path using C# .
Thanks in advance.
All Replies
-
Thursday, March 06, 2008 6:27 AM
Hi sunDAC,
As far as I know, you can change the DataSourceConnections property of the report document to change the database, and here is a article with a sample concerns this problem, please check the URL below.
http://www.codeproject.com/KB/vb/Crystal_Rpt__Connectio.aspx
Regards,
Xun
-
Thursday, March 06, 2008 7:56 AM
HI Xun,
Thanks for your reply.
I am fresher for crystalreport and using C#.
Your code is using VB.I dont really hand on VB.
Is it possible for you to give the procedure using C#.
Waiting for your reply.
With Regards
sunDAC -
Thursday, March 06, 2008 8:30 AM
Hi
Please check the C# code for assign database connection for the crystal report below.
Code Snippet//File: Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace CrystalReport_Change_connection
{
public partial class Form1 : Form
{
CrystalReport1 rpt = new CrystalReport1();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string path1 = @"E:\Crystal_Repost_Sample\test.mdb";
try
{
rpt.DataSourceConnections[0].SetConnection("", path1, false);
// if password is given then give the password
// if not give it will ask at runtime
rpt.DataSourceConnections[0].SetLogon("", "");
crystalReportViewer1.ReportSource = rpt; //assign the report to viewr
}
catch(CrystalDecisions.ReportSource.EnterpriseLogonException exp)
{
MessageBox.Show(exp.Message);
}
}
}
}
Regards,
Xun
-
Thursday, March 06, 2008 9:08 AM
Hi,
I get all the things, But i cant understand
string path1 = @"E:\Crystal_Repost_Sample\test.mdb";
Is this path1 is path of database.
I am using SQL databse
I am giving my code here
using
System;using
System.Collections;using
System.ComponentModel;using
System.Data;using
System.Data.SqlClient;using
System.Drawing;using
System.Web;using
System.Web.SessionState;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.HtmlControls;using
CrystalDecisions.Shared;using
CrystalDecisions.CrystalReports.Engine;using
System.Configuration;public
partial class Report_BT_View_LogSheetNumber : System.Web.UI.Page{
DateTime fromdate, todate; string MediaCode; ReportDocument reportDocument = new ReportDocument(); ParameterField paramField = new ParameterField(); ParameterFields paramFields = new ParameterFields(); ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue(); protected void Page_Load(object sender, EventArgs e){
try{
fromdate =
Convert.ToDateTime(Request.QueryString["fromdate"].ToString().Trim());todate =
Convert.ToDateTime(Request.QueryString["todate"].ToString().Trim());MediaCode = Request.QueryString[
"MediaCode"].ToString().Trim(); // ********************** For Report **************************************************paramField.Name =
"@FromDate";paramDiscreteValue.Value = fromdate;
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
paramField =
new ParameterField();paramField.Name =
"@ToDate";paramDiscreteValue =
new ParameterDiscreteValue();paramDiscreteValue.Value = todate;
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
paramField =
new ParameterField();paramField.Name =
"@MediaCode";paramDiscreteValue =
new ParameterDiscreteValue();paramDiscreteValue.Value = MediaCode;
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
//*********************Assign all parameters to store procedure for crystalreportviewerCrystalReportViewer1.ParameterFieldInfo = paramFields;
//************************set the report path***********************reportDocument.Load(Server.MapPath(
"BT_rpt_LogSheetNumber.rpt")); //**********************dynamic databse login********************************************* ConnectionInfo connection = new ConnectionInfo();connection.DatabaseName = Application[
"DBName"].ToString();connection.ServerName = Application[
"DBServer"].ToString();connection.UserID = Application[
"DBUserId"].ToString();connection.Password = Application[
"DBPassword"].ToString(); //******************** First we assign the connection to all tables in the main report foreach (CrystalDecisions.CrystalReports.Engine.Table table in reportDocument.Database.Tables){
//*********************************Cache the logon info block TableLogOnInfo logOnInfo = table.LogOnInfo; //*************************************Set the connectionlogOnInfo.ConnectionInfo = connection;
//******************************Apply the connection to the table!table.ApplyLogOnInfo(logOnInfo);
}
//*****************************Verify Database***************************reportDocument.VerifyDatabase();
//*************************Other way to login only for this report and allready set databse crystal desig*********************** //reportDocument.SetDatabaseLogon("Rise_n_shine", "Rise_n_shine"); //reportDocument.SetDatabaseLogon(Application["DBUserId"].ToString(), Application["DBPassword"].ToString()); //********************* This is another method for set database path //reportDocument.SetDatabaseLogon("Samvaad", "Samvaad", "m3", "Samvaad", false); //******************************Load the report by setting the report sourceCrystalReportViewer1.ReportSource = reportDocument;
}
catch (Exception ex){
Trace.Warn(ex.Message.ToString());
Trace.Warn(ex.Source.ToString());
Trace.Warn(ex.TargetSite.ToString());
}
}
}
Is this correct , i dont know what to do
Hopfully u understand my problem.
What is ultimte solution for this type of problem.
Please help me.
Waiting For reply
With Regards,
SunDAC
-
Friday, April 11, 2008 8:09 AM
you can pass database informations dynamically through Crstal Reports ConnectionInfo
With crConnectionInfo
.ServerName = "YOUR SERVER NAME"
.DatabaseName = "YOUR DATABASE NAME"
.UserID = "YOUR DATABASE USERNAME"
.Password = "YOUR DATABASE PASSWORD"
End Withhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_load_dynamically.htm
-
Monday, April 14, 2008 5:42 AM
Hi fami,
Thanks for the reply.I am going through the link.It gives really information.But sorry i am really unaware of VB dot net syntax.
Your using With End With. But i think it is not available for C#.
I am giving my code below.
It works fine on my pc.But when i want to use on my server it creates problem.
Is it possible to you to give procedure in C#
reportDocument.Load(Server.MapPath(
"MK_rpt_PotentialClientList.rpt"));
//**********************dynamic databse login********************************************* ConnectionInfo connection = new ConnectionInfo();connection.DatabaseName = Application[
"DBName"].ToString();connection.ServerName = Application[
"DBServer"].ToString();connection.UserID = Application[
"DBUserId"].ToString();connection.Password = Application[
"DBPassword"].ToString();
//******************** First we assign the connection to all tables in the main report foreach (CrystalDecisions.CrystalReports.Engine.Table table in reportDocument.Database.Tables){
//*********************************Cache the logon info block TableLogOnInfo logOnInfo = table.LogOnInfo; //*************************************Set the connectionlogOnInfo.ConnectionInfo = connection;
//******************************Apply the connection to the table!table.ApplyLogOnInfo(logOnInfo);
}
//*****************************Verify Database***************************reportDocument.VerifyDatabase();
//******************************Load the report by setting the report sourceCrystalReportViewer1.ReportSource = reportDocument;
Waiting for the reply.
-
Tuesday, May 06, 2008 1:55 PMHi am created backup and restore in sql database..but i want in msaccess backup and restore
how i can do in c#
i am attach in sql code
please reply me
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Management;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace Accesspath
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection("Integrated security=true;Initial catalog=master");
SqlConnection cn = new SqlConnection("Integrated security=true;Initial catalog=master");
private void btbrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "Select Folder";
dialog.RootFolder = Environment.SpecialFolder.MyComputer;
dialog.ShowNewFolderButton = false;
dialog.SelectedPath = @"C:\My Computer";
if (dialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = dialog.SelectedPath;
}
else
{
textBox1.Text = "";
}
}
private void btchange_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("Backup database prem to disk='"+textBox1.Text+"//ma.bak'");
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Inserted");
}
private void btrestore_Click(object sender, EventArgs e)
{
SqlCommand cmd=new SqlCommand("Restore database prem from disk='"+textBox1.Text+"//ma.bak'");
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Values are restored");
}
}
}

