Question:
how to save data from datagrid to the sql database table.?
shraddha
2009-03-02 01:02:16 UTC
i need code to add data from datagrid to the sqlserver database...?
how to do it..?
Four answers:
ganesh d
2009-03-02 03:26:18 UTC
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.
Madboy P
2009-03-02 01:29:15 UTC
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
Mona
2009-03-02 01:54:25 UTC
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.
?
2016-04-11 11:44:40 UTC
Verify if the value is null using the ISDBNull Function


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...