Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET4.0
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 6-Jun-12 23:00pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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!
  Permalink  
Comments
Manas Bhardwaj at 8-Jun-12 3:40am
   
well said. +5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Check if the signature of the method is as follows
 
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
  Permalink  
v2
Comments
nicky_008 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 403
1 OriginalGriff 354
2 George Jonsson 202
3 Shemeemsha RA 148
4 Animesh Datta 130
0 OriginalGriff 6,189
1 Sergey Alexandrovich Kryukov 5,666
2 CPallini 4,810
3 George Jonsson 3,429
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 8 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100