Click here to Skip to main content
15,910,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
here is my code that i am trying to display the data in grid view using store procedure for windows application,but i got stuck here...
C#
con = new SqlConnection("server=(local);database=sp;user id=sa;password =******");
           con.Open();
           cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.CommandText = "P_PB_Getall"; // store procedure name 
           dataGridView1.DataSource = ; // here what should i write the code to display  the data in the grid view... 

           con.Close();
Posted
Updated 9-May-16 23:41pm

add this,

C#
Datatable dt = new Datatable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
datagridview1.Bind();
con.close();
 
Share this answer
 
v2
C#
int id = Convert.ToInt32(comboBox2.SelectedValue.ToString());
    //int idc = 100;
    DataSet ptDataset = new DataSet();
    string con = ConfigurationManager.ConnectionStrings["secaloFormulaCS"].ToString(); 
    SqlConnection sqlCon = new SqlConnection(con);
    sqlCon.Open();
    SqlCommand sqlCmd = new SqlCommand("spDispInformation", sqlCon);
    sqlCmd.CommandType = CommandType.StoredProcedure;
    sqlCmd.Parameters.AddWithValue("@id", id);
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    da.Fill(ptDataset);
    dataGridView2.DataSource =  ptDataset.Tables[0];
    sqlCon.Close();
 
Share this answer
 
v2
Try this-
C#
con = new SqlConnection("server=(local);database=sp;user id=sa;password =******");
           con.Open();
           cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.CommandText = "P_PB_Getall"; 
           Datatable dt = new Datatable();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt);
           dataGridView1.DataSource = dt; 
           con.Close();


Hope, it helps :)
 
Share this answer
 
v3
Comments
Gowtham Gururaj 2-Nov-15 4:54am    
Ya it worked...Thanks :) for the solution
Suvendu Shekhar Giri 2-Nov-15 5:17am    
Glad that it helped :)

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