Click here to Skip to main content
15,881,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created button on Frontend. Button Name: Show Records

I have two tables created in Database

First Table Name "A" in this table "A" ID is PrimaryKey
Second Table Name: "B" in this "B" table User_Id is first table of PrimaryKey in second table Foreignkey

if A table ID = B Table User_Id is same.

(above condition i created StoredProcedure using JOIN Clause)
Stored ProcedureName: SP_GetDetails. @userid 1
after execute StoreProcedure.
12 records show in SQL Database.

based on above stored Procedure, when user click on Show Records Button, show 12 records in gridView

What I have tried:

C#
private void ShowRcords_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=\\SQLEXPRESS;Database=CSharpUserLogin; Integrated Security=true");
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_GetUserDetail", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@userId", SqlDbType.VarChar,50).Value = txtId.Text);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataTable dt = new DataTable();
            sda.Fill(dt);
            BindingSource bs = new BindingSource();
            bs.DataSource = dt;
            dgvEmployee.DataSource = bs;
            sda.Update(dt);
Posted
Updated 31-Dec-21 3:49am
v2
Comments
PIEBALDconsult 31-Dec-21 9:51am    
This looks like are post of
https://www.codeproject.com/Questions/5321154/How-to-call-stored-PROCEDURE-in-Csharp-code

1 solution

0) My suspicion is that the datatable you're binding to goes out of scope when the method exits and becomes null, thus forcing the bound control to display no rows.

1) Calling sda.Update is not necessary if you're just retrieving data.

2) I never bind directly to a DataTable. Instead use the MVVM pattern to make binding less messy and more convenient.

3) Do the binding in the XAML.

4) Are you sure your sql parameter is really a string, and not an integer? That's highly weird.

5) My method would look something like this:

C#
SqlConnection con = null;
SqlCommand    cmd = null;
string text = "1234";
DataTable     dt  = new DataTable();
try
{
    using (con = new SqlConnection("Data Source=\\SQLEXPRESS;Database=CSharpUserLogin; Integrated Security=true"))
    {
        using (cmd = new SqlCommand("sp_GetUserDetail", con){ CommandType=CommandType.StoredProcedure })
        {
            cmd.Parameters.Add(new SqlParameter("@userId", text));
            SqlDataAdapter sda = new SqlDataAdapter(){ SelectCommand = cmd };
            con.Open();
            sda.Fill(dt);
        }
    }
}
catch (Exception ex)
{
    // do something with exception
}
 
Share this answer
 
v2

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