Click here to Skip to main content
14,880,257 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
data save in database from datagridview
Posted
Comments
ajay251987 22-Nov-13 0:06am
   
hey u can first get the connection string and then try this below code

for(int i=0; i< dataGridView1.Rows.Count-1;i++)
{
Strquery= @"Insert into yourTableName(date,column1,....) values ("
+ System.DateTime.Now +", "
+ dataGridView1.Rows[i].Cells[1].Value +".......);";
cmd.CommandText = Strquery;
cmd.ExecuteNonQuery();
}

try this dude:
VB
''Declare this to your class
 Public Const CONNECTION_STRING As String = "Data Source=YOUR_HOSTNAME;Initial Catalog=DATABASE_NAME; Integrated Security=True"
    Dim dbConnection As SqlConnection = New SqlConnection(CONNECTION_STRING)
Dim ds As DataSet = New DataSet
Dim da As SqlDataAdapter
''//Declare this to your class

 Private Sub updateDGV() 
        Dim SQLText As String = "SELECT Field1,Field2,Field3,Field4,Field5 FROM TEST"
 
        Try
            If dbConnection.State = ConnectionState.Open Then dbConnection.Close()
            dbConnection.Open()
            da= New SqlDataAdapter(SQLText, dbConnection)
            da.Fill(ds, "TEST")
            dbConnection.Close()
 
        Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
        da.Update(ds, "TEST")
     
       Catch ex As Exception
            MessageBox.Show(ex.ToString)
       End Try
  
  End Sub


Call the updateDGV method to your Save button and make sure your datagrid datasource is the ds declared . Hope this will help you.
   

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