Click here to Skip to main content
15,124,736 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
In these code parameters how will be initializing?

C#
con = new SqlConnection(
  "Data Source=mysource;Initial Catalog=mydbname;uid=sa");
//create sql adapter for the "emp" table

SqlDataAdapter sqlDa = new SqlDataAdapter("select * from emp", con);
//create dataset instance

DataSet    dSet = new DataSet();
//fill the dataset

sqlDa.Fill(dSet, "emp");
//bind the data grid with the data set

dataGrid1.DataSource=dSet.Tables["emp"];

//build select command

SqlCommand selCmd = new SqlCommand("select * from emp",con);
sqlDa.SelectCommand=selCmd;

//build insert command

SqlCommand insCmd = new SqlCommand(
  "insert into emp (Name, Age) values(@Name, @Age)",con);
insCmd.Parameters.Add("@Name", SqlDbType.NChar, 10, "Name");  //such as here
insCmd.Parameters.Add("@Age", SqlDbType.Int, 4, "Age");
sqlDa.InsertCommand = insCmd;

//build update command

SqlCommand upCmd = new SqlCommand(
  "update emp set Name=@Name, Age=@Age where No=@No",con);
upCmd.Parameters.Add("@Name", SqlDbType.NChar, 10, "Name");
upCmd.Parameters.Add("@Age", SqlDbType.Int, 4, "Age");
upCmd.Parameters.Add("@No", SqlDbType.Int, 4, "No");
sqlDa.UpdateCommand = upCmd;

//build delete command

SqlCommand delCmd = new SqlCommand(
  "delete from emp where No=@No",con);
delCmd.Parameters.Add("@No", SqlDbType.Int, 4, "No");
sqlDa.DeleteCommand = delCmd;

//now update the data adapter with dataset.

sqlDa.Update(dSet,"emp");
Posted
Updated 7-Mar-11 9:42am
v4
Comments
fjdiewornncalwe 7-Mar-11 14:48pm
   
Could you expand on what your question is? I'm not sure we can make it out from just the code like this.

1 solution

Why are you updating dSet, and what with?

You haven't done anything with the data you fetched into dSet initially and displayed in your DataGridView. So why try to update?

Once again, this information is freely available on the internet if only you would be prepared to search for it.
   

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