Results 1 to 3 of 3

Thread: XML Schema Default Location

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    10

    Question 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 customName:  Screenshot 2021-02-09 180251.jpg
Views: 144
Size:  38.7 KBized 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();

    }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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:
    1. var filePath = Path.Combine(Application.StartupPath, @"subfolder\file.ext");
    This brings up another issue with your code. This is wrong:
    csharp Code:
    1. 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:
    1. 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:
    1. ds.WriteXmlSchema(@"D:\cdr_schema");

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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:
    1. {
    2.             string myconstring = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString;
    3.  
    4.             SqlConnection con1 = new SqlConnection(myconstring);
    5.  
    6.             DataSet ds = new DataSet();
    7.             DataTable dt = new DataTable();
    8.  
    9.             dt.Columns.Add("CustId", typeof(string));
    10.             dt.Columns.Add("CustName", typeof(string));
    11.             dt.Columns.Add("Total", typeof(decimal));
    12.  
    13.             foreach(DataGridViewRow dgv in dgv_max_purchase.Rows)
    14.             {
    15.                 dt.Rows.Add(dgv.Cells[0].Value,dgv.Cells[1].Value,dgv.Cells[2].Value);
    16.             }
    17.  
    18.             ds.Tables.Add(dt);
    19.             ds.WriteXmlSchema("D:\cdr_schema");  // ===>> I customized the code but it is not working
    20.  
    21.             Report rpt = new Report();
    22.             CrystalReport1 crt = new CrystalReport1();
    23.  
    24.             TextObject text = (TextObject)crt.ReportDefinition.Sections["Section1"].ReportObjects["text24"];
    25.             TextObject text1 = (TextObject)crt.ReportDefinition.Sections["Section1"].ReportObjects["text25"];
    26.             TextObject text2 = (TextObject)crt.ReportDefinition.Sections["Section1"].ReportObjects["text26"];
    27.            
    28.             text.Text = Convert.ToString(dtfrom.Value);
    29.             text1.Text = Convert.ToString(dtto.Value);
    30.             text2.Text = lb_branch.Text;
    31.  
    32.             crt.SetDataSource(ds);
    33.             rpt.crystalReportViewer1.ReportSource = crt;
    34.             rpt.crystalReportViewer1.Refresh();
    35.            
    36.             rpt.Show();
    37.  
    38. }
    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
  •  



Click Here to Expand Forum to Full Width