Hi,
My application is in VS2008 coded in VB.Net
I have a DatagridView.I have a Save Button on my Form.Now my requirement is i want to the Data that is populated in Datagridview when i click on Save button.
I want to do it using Update Query.
below is my code.
DgvStudRecord is my DatagridView
I want to update only the Fourth Column in Datagrid.

Sub update()
        DBConnect()
        con = DBActions.DBConnect
        strsql = "Update AdmittedStudents set MobileNo='"  DgvStudRecord.Item(4,    DgvStudRecord.CurrentCell.RowIndex)  "' where RegNo='" & 1 & "'"
        cmd = New SqlCommand(strsql, con)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Record Updated", "Weekly Off", MessageBoxButtons.OK)
    End Sub

Im getting an error in the Query as End Of Statement Expected.This is the first time transacting using DatagridView.
Please suggest at the earliest where im wrong.

Recommended Answers

All 4 Replies

Before executing the Query i.e. cmd.ExecuteNonQuery()
Get the commandstring i.e:strsql and paste it on ur SQL server and see whether the statement is formated properly or not.

hi,
Yes i have tried that,the Query i executing fine.I have tried the same query using different controls and its working fine.Its giving me problem only when im using DatgridView

Can you paste the code what your getting into strsql before running cmd.ExecuteNonQuery()

Hi i have got my solution.
strsql = "Update AdmittedStudents set MobileNo='" & DgvStudRecord.Item(4, DgvStudRecord.CurrentCell.RowIndex) & "' where RegNo='" & 1 & "'"

This is exactly what should be done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.