Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a DataAdapter with stored procedure and parameter

I want to fill a DataGridView control using DataAdapter. But I don't know how to do it since I'm using a stored procedure with parameter. Can someone cite an example please?

like image 619
yonan2236 Avatar asked Aug 20 '10 05:08

yonan2236


People also ask

Do we use stored procedure in Ado net?

Executing a Stored ProcedureUsing a stored procedure with ADO.NET is easy. You simply follow four steps: Create a Command , and set its CommandType property to StoredProcedure . Set the CommandText to the name of the stored procedure.

Which method of a DataAdapter is used to populate a DataSet?

The Fill method of the DataAdapter is used to populate a DataSet with the results of the SelectCommand of the DataAdapter . Fill takes as its arguments a DataSet to be populated, and a DataTable object, or the name of the DataTable to be filled with the rows returned from the SelectCommand .


2 Answers

I got it!...hehe

protected DataTable RetrieveEmployeeSubInfo(string employeeNo)         {             SqlCommand cmd = new SqlCommand();             SqlDataAdapter da = new SqlDataAdapter();             DataTable dt = new DataTable();             try             {                 cmd = new SqlCommand("RETRIEVE_EMPLOYEE", pl.ConnOpen());                 cmd.Parameters.Add(new SqlParameter("@EMPLOYEENO", employeeNo));                 cmd.CommandType = CommandType.StoredProcedure;                 da.SelectCommand = cmd;                 da.Fill(dt);                 dataGridView1.DataSource = dt;             }             catch (Exception x)             {                 MessageBox.Show(x.GetBaseException().ToString(), "Error",                         MessageBoxButtons.OK, MessageBoxIcon.Error);             }             finally             {                 cmd.Dispose();                 pl.MySQLConn.Close();             }             return dt;         } 
like image 76
yonan2236 Avatar answered Sep 17 '22 15:09

yonan2236


 SqlConnection con = new SqlConnection(@"Some Connection String");  SqlDataAdapter da = new SqlDataAdapter("ParaEmp_Select",con);             da.SelectCommand.CommandType = CommandType.StoredProcedure;             da.SelectCommand.Parameters.Add("@Contactid", SqlDbType.Int).Value = 123;             DataTable dt = new DataTable();             da.Fill(dt);             dataGridView1.DataSource = dt; 
like image 23
Chandra Malla Avatar answered Sep 18 '22 15:09

Chandra Malla