0
votes

i successfully designed and filled my Crystal report via code not via wizard.

  1. i added Crystal report via addNEWITEM
  2. i added dataset in aap_code via addNEWITEM
  3. i added datatable into dataset via addNEWITEM
  4. Via code i made report and filled dataset and table with data
  5. Run and display. Successfully done.

but my question is that how to do it fully via code like for steps 1,2,3 ? I don't want to add it via AddNewItem etc, isn't there any way to add these via code ? i did, i created some datasets and table via code like we do for Gridview etc but that doesn't appear in DATA connections of crystal report etc.

String conStr =WebConfigurationManager.ConnectionStrings["LoginDatabaseConnectionString"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) 
    {
        Dataset_load();
    }
}

protected void Dataset_load() 
{
    SqlConnection sqlcon = new SqlConnection(conStr);
    SqlCommand sqlCom = new SqlCommand("select * from Login", sqlcon);
    SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom);
   // DataSet ds = new DataSet("CRDataSet");

    try
    {
        sqlcon.Open();
        //sqlCom.ExecuteNonQuery();
        //sqlDA.Fill(ds,"Login");

        DataSet1 ds = new DataSet1();
        DataTable dt = new DataTable("DT_CR");
        sqlDA.Fill(dt);
        ds.Tables[0].Merge(dt);

        ReportDocument rd = new ReportDocument();
        rd.Load(Server.MapPath("CrystalReport.rpt"));
        rd.SetDataSource(ds);
        CrystalReportViewer1.ReportSource = rd;


    }
    catch (Exception exc)
    {
        Response.Write(exc.Message);
    }
    finally 
    {
        sqlcon.Close();
    }
1
You have your answer right in front of you. You have all the code...that's how you do it. ;)campagnolo_1
yes i know, thanks but i'm asking about steps 1,2,3 ? i mean that all is clicking add new item adn then drad and drop then setting it's database connection etc, so ANY WAY TO DO IT VIA CODE ?Hunain
want to do it because to improve my coding, Drag and drop are for those who are at professional level and doesn't have time but i'm looking to improve my codeHunain
Well, see my answer below. I think that would be a good way to get an idea of all the pieces you need to make it work. See what's needed, how it's built and then you can adapt it to your needs. Good for you to try and learn it that way!campagnolo_1
ok got it bro, thanks but one more thing, i added an empty dataset DATASET1 into APPCODE folder and then dummy Datatable, DATATABLE1 with dummy column names same as it is in Database but while writing code, what does this mean " ds.Tables[0].Merge(dt) " what does index 0 represents here and what actually happened to that dummy datatable that i created ?Hunain

1 Answers

0
votes

I'm not sure why you would make it harder on yourself by trying to create all the code from scratch rather than using the functionality of VS. If you really want to do it though, I would create everything using the built-in functionality. Then I would look at all the code that gets added and see what does what. There are some tutorials out there that tell you how to do it using the features. Create one and then read through the code.

That's how I would do it.

Hope that helps,

Chris