Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i'm trying to display data in crystal report , I try this c# code but I want to use stored procedure : this is my initial code without stored procedure:
C#
protected DataTable EmployeeDetails()
{
    string id = IDEmployee;
 
    string sql = "sql Query ";
    SqlDataAdapter dad = new SqlDataAdapter(sql, con);
    DataSetAttendance ds = new DataSetAttendance();
    dad.Fill(ds.Tables["MST_Employee"]);
    DataTable employees = ds.Tables["MST_Employee"];
    return employees;
}

C#
private void PermissionReportViewer_Load(object sender, EventArgs e)
{
    try
    {
        con.ConnectionString = @"ConnectionString";      

        DataSet ds = new DataSet();
        DataTable dt1 = EmployeeDetails().Copy();

        ds.Tables.Add(dt1);
        Reports.PermissionReport myreport = new Reports.PermissionReport();
        myreport.SetDataSource(ds);
        crystalReportViewer1.ReportSource = myreport;
        crystalReportViewer1.Refresh();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error" +ex.ToString(), "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);        
    }
}

My code with stored procedure:
C#
protected DataTable EmployeeDetails()
{
    string id = IDEmployee;

    con.Open();
    SqlCommand sqlComm = new SqlCommand("GetEmployeeDetails", con);

    sqlComm.Parameters.AddWithValue("@ID", id);
   
    sqlComm.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter dad = new SqlDataAdapter();
    dad.SelectCommand = sqlComm;
    DataSetAttendance ds = new DataSetAttendance();
    dad.Fill(ds, "MST_Employee");
    DataTable total = ds.Tables["MST_Employee"];
    con.Close();
    return total;
}

but it doesn't work !! Any Solution please ?
Posted
Updated 13-Jun-15 2:55am
v6
Comments
Mathi Mani 13-Jun-15 14:38pm    
What do mean by does not work? What is issue or error message?
Member 10286520 14-Jun-15 12:03pm    
thanks Mathi for your reply, it wad just an error in my stored procedure. the code is correct.

1 solution

Hello -

You dont need to open/close conn with Data Adapater; try this:

C#
protected DataTable EmployeeDetails()
{
    string id = IDEmployee;
    SqlDataAdapter dad = new SqlDataAdapter("GetEmployeeDetails", con);
    dad.SelectCommand.CommandType = CommandType.StoredProcedure;
    dad.SelectCommand.Parameters.Add("@ID", SqlDbType.NVarChar).Value = id;
    
    DataTable total = ds.Tables["MST_Employee"];
    dad.Fill(total, "MST_Employee");
    return total;
}


Thanks,

Kuthuparakkal
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900