Monday, August 16, 2010

How to save data from datagrid to the sql database table.?

i need code to add data from datagrid to the sqlserver database...?


how to do it..?How to save data from datagrid to the sql database table.?
Hi shraddha,


Solution for Ur Problem Dear.





To Retrieve Data in to Datagrid use this :





Dim con As SqlConnection


Dim da As SqlDataAdapter


Dim ds As New DataSet


Dim cb As SqlCommandBuilder


con = New SqlConnection(';data source=Localhost;initial catalog=databasename;user id=sa;password=;';)


da = New SqlDataAdapter(';select * from tablename';, con)


da.Fill(ds, ';tablename';)


DataGrid1.DataBindings.Add(';text';, ds, ';tablename';)


DataGrid1.DataSource = ds.Tables(';tablename';)


cb = New SqlCommandBuilder(da)








and when u want to update the edited fields from datagrid to database use this code on button click event:





da.Update(ds, ';tablename';)





this will help u dear .





bye.How to save data from datagrid to the sql database table.?
Datagrid control can be connected to a data base. If you are using the VB or VB.NET, then you can use the property table, so that you can define the connection details such as the DB type, username, password etc. Then you can use the datagrid control apis, which inturn will update the data in the backend sql database
conn.Open();


SqlDataAdapter adap1 = new SqlDataAdapter(';Delete from TableName WHERE FieldName = 1';, cn);


DataSet ds1 = new DataSet();


adap.Fill(ds1);


dataGridView1.DataSource = ds1.Tables[0];


conn.Close();





This is in C#. Try to convert it into VB.

No comments:

Post a Comment