-
Feb 9th, 2021, 10:04 AM
#1
Thread Starter
New Member
XML Schema Default Location
Hi There!
I am building an application (c#) which shows report with data from sql DB. It is working on my Computer. I am using crystal report for showing report with xml schema.
Dataset is working fine but when I am publishing the application and installing it on other computer the application is not able to find dataset(xml) as its default location is set as per my main computer.
Can someone help me out to set application automatically look for dataset from the computer, with new location on which the application is installed.
I am using Visual Studio 2019 with.0 fmw. Below is my code:
{
string myconstring = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString;
SqlConnection con1 = new SqlConnection(myconstring);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("CustId", typeof(string));
dt.Columns.Add("CustName", typeof(string));
dt.Columns.Add("Total", typeof(decimal));
foreach(DataGridViewRow dgv in dgv_max_purchase.Rows)
{
dt.Rows.Add(dgv.Cells[0].Value,dgv.Cells[1].Value,dgv.Cells[2].Value);
}
ds.Tables.Add(dt);
ds.WriteXmlSchema("D:\cdr_schema"); ===>> I customized the code but it is not working
Report rpt = new Report();
CrystalReport1 crt = new CrystalReport1();
TextObject text = (TextObject)crt.ReportDefinition.Sections["Section1"].ReportObjects["text24"];
TextObject text1 = (TextObject)crt.ReportDefinition.Sections["Section1"].ReportObjects["text25"];
TextObject text2 = (TextObject)crt.ReportDefinition.Sections["Section1"].ReportObjects["text26"];
text.Text = Convert.ToString(dtfrom.Value);
text1.Text = Convert.ToString(dtto.Value);
text2.Text = lb_branch.Text;
crt.SetDataSource(ds);
rpt.crystalReportViewer1.ReportSource = crt;
rpt.crystalReportViewer1.Refresh();
rpt.Show();
}
-
Feb 9th, 2021, 07:05 PM
#2
Re: XML Schema Default Location
In a WinForms application, use Application.StartupPath to get the path of the folder from which the current executable was run. You can then use Path.Combine to create a subfolder and/or file path, e.g.
csharp Code:
var filePath = Path.Combine(Application.StartupPath, @"subfolder\file.ext");
This brings up another issue with your code. This is wrong:
csharp Code:
ds.WriteXmlSchema("D:\cdr_schema");
The backslash is the escape character in C# so that is actually creating a string containing "D:", followed by the special character '\c', followed by "dr_schema", which is obviously not what you intended.
In C# and other C-based languages, you need to escape a literal backslash with another backslash, e.g.
csharp Code:
ds.WriteXmlSchema("D:\\cdr_schema");
C# also supports verbatim string literals, where there are no escape characters and backslashes are treated just like any other character. I used one in my first example, so your existing code could look like this:
csharp Code:
ds.WriteXmlSchema(@"D:\cdr_schema");
-
Feb 9th, 2021, 07:08 PM
#3
Re: XML Schema Default Location
Also, please format your code properly when posting. The fixed-width font and particularly the indenting makes it easier to read that way.
csharp Code:
{
string myconstring = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString;
SqlConnection con1 = new SqlConnection(myconstring);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("CustId", typeof(string));
dt.Columns.Add("CustName", typeof(string));
dt.Columns.Add("Total", typeof(decimal));
foreach(DataGridViewRow dgv in dgv_max_purchase.Rows)
{
dt.Rows.Add(dgv.Cells[0].Value,dgv.Cells[1].Value,dgv.Cells[2].Value);
}
ds.Tables.Add(dt);
ds.WriteXmlSchema("D:\cdr_schema"); // ===>> I customized the code but it is not working
Report rpt = new Report();
CrystalReport1 crt = new CrystalReport1();
TextObject text = (TextObject)crt.ReportDefinition.Sections["Section1"].ReportObjects["text24"];
TextObject text1 = (TextObject)crt.ReportDefinition.Sections["Section1"].ReportObjects["text25"];
TextObject text2 = (TextObject)crt.ReportDefinition.Sections["Section1"].ReportObjects["text26"];
text.Text = Convert.ToString(dtfrom.Value);
text1.Text = Convert.ToString(dtto.Value);
text2.Text = lb_branch.Text;
crt.SetDataSource(ds);
rpt.crystalReportViewer1.ReportSource = crt;
rpt.crystalReportViewer1.Refresh();
rpt.Show();
}
It would also help if you removed the useless leading whitespace. You can do that by holding down the Alt key while selecting text in VS. That will select an arbitrary rectangular block of code, not selecting every line from the very beginning.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|