Abdussalam Mazhar
Abdussalam Mazhar

Reputation: 57

c# gridview not populating stored procedure sql

I am trying to populate the gridview with a stored procedure with the following code but whats happening is the gridview is not showing anything.

SqlConnection myConnectiona = new SqlConnection("user id=HOME-PC\\HOME;" +
                                   "password=password;server=HOME-PC\\SQLEXPRESS;" +
                                   "Trusted_Connection=yes;" +
                                   "database=tabrem; " +
                                   "connection timeout=30");

            SqlCommand pro = new SqlCommand("[dbo].[doctor]", myConnectiona);

            pro.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(pro);

            DataTable dt = new DataTable();



        try
        {
                    myConnectiona.Open();

          da.Fill(dt);
          dataGridView1.DataSource = dt;


        }
        catch (Exception w)
        {
            throw;
        }
        finally
        {
            if (myConnectiona.State == ConnectionState.Open)
                myConnectiona.Close();
        }

Upvotes: 0

Views: 48

Answers (1)

zybroxz
zybroxz

Reputation: 713

I think you need to do (if this is web forms)

da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Databind();

thanks

Upvotes: 1

Related Questions