Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Protected Sub GridView1_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim ID As String = GridView1.Rows(e.RowIndex).Cells(0).Text

        Dim Company As String = (CType(GridView1.Rows(e.RowIndex).Cells(1).Controls(0), TextBox)).Text
        Dim Name As String = (CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox)).Text
        Dim Title As String = (CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox)).Text
        Dim Address As String = (CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox)).Text
        Dim Country As String = (CType(GridView1.Rows(e.RowIndex).Cells(5).Controls(0), TextBox)).Text

        Dim UpdateOrAddNewRecord(ID, Company, Name, Title, Address, Country, True)
        GridView1.EditIndex = -1
        BindGridView()
    End Sub
Posted

Do you really use a string (i.e. char, nchar, varchar, or nvarchar) for the ID column??? I would rather expect it to be an integer with "identity" set to true, i.e. an automatically generated value on the server.
And normally, SQLServer won't allow you to insert the identity value (except when you explicitly changed that setting).
And an empty string is never a valid integer value.
I know that MySQL allows the insertion of identity values by default - and when you use "0", it replaces that with an automatically generated value (but I do not know how it treats an empty string in that place).
But since you are using SQLServer, change your code: do not use the ID value in the INSERT statement!
 
Share this answer
 
Comments
Manas Bhardwaj 8-Jun-12 3:40am    
well said. +5
Check if the signature of the method is as follows

VB
Dim UpdateOrAddNewRecord(Dim ID As String,Dim  Company As String , Dim Name as String,Dim Title as String,Dim Address As String,Dim Country as String,Dim flag as booean)


Either you need to change the signature of this method or the datatypes declared in the above code as per your functionality
 
Share this answer
 
v2
Comments
nicky_008 7-Jun-12 5:26am    
i already did these thing....please check....following code....thankss in advance... Private Sub UpdateOrAddNewRecord(ByVal ID As String, ByVal Company As String, ByVal Name As String, ByVal Title As String, ByVal Address As String, ByVal Country As String, ByVal isUpdate As Boolean)
Dim connection As SqlConnection = New SqlConnection("Data Source=KARANDE\sqlexpress;Initial Catalog=nitink;Integrated Security=True")
Dim sqlStatement As String = String.Empty

If Not isUpdate Then
sqlStatement = "INSERT INTO Customer" + "(ID,Company,Name,Title,Address,Country)" +
"VALUES (@ID,@Company,@Name,@Title,@Address,@Country)"
Else
sqlStatement = "UPDATE Customer" + "SET Company = @Company,Name = @Name," + "Title = @Title,Address = @Address,Country = @Country" + "WHERE ID = @ID,"
End If
Try
connection.Open()
Dim cmd As SqlCommand = New SqlCommand(sqlStatement, connection)
cmd.Parameters.AddWithValue("@ID", ID)
cmd.Parameters.AddWithValue("@Company", Company)
cmd.Parameters.AddWithValue("@Name", Name)
cmd.Parameters.AddWithValue("@Title", Title)
cmd.Parameters.AddWithValue("@Address", Address)
cmd.Parameters.AddWithValue("@Country", CounTry)
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
Catch ex As System.Data.SqlClient.SqlException
Dim msg As String = "Insert/Update Error:"
msg += ex.Message
Throw New Exception(msg)

Finally
connection.Close()
End Try
End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
UpdateOrAddNewRecord(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, False)

BindGridView()

End Sub

Protected Sub GridView1_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex ' turn to edit mode
BindGridView()

End Sub

Protected Sub GridView1_RowCancelingEdit(sender As Object, e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1 'swicth back to default mode
BindGridView()

End Sub


Protected Sub GridView1_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

Dim ID As String = GridView1.Rows(e.RowIndex).Cells(0).Text

Dim Company As String = (CType(GridView1.Rows(e.RowIndex).Cells(1).Controls(0), TextBox)).Text
Dim Name As String = (CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox)).Text
Dim Title As String = (CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox)).Text
Dim Address As String = (CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox)).Text
Dim Country As String = (CType(GridView1.Rows(e.RowIndex).Cells(5).Controls(0), TextBox)).Text

Dim UpdateOrAddNewRecord(ID, Company, Name, Title, Address, Country, True)
GridView1.EditIndex = -1
BindGridView()
End Sub

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