Update using datagrid

asp.net United States
  • 18 years ago

    Hi guys, please help me on this.
    I have a web form datagrid with a few columns and a editcolumn. I had populated the datagrid and was trying to update certain records... somehow the update command didnt execute appropriately, and I don't know why. here's some of the codes.


    'this is the bind function


       Sub bind()


           Dim dept As String
           dept = Me.cmbDept.SelectedItem.Text
           Dim datetemp As String
           datetemp = Me.txtpdate.Text


           conn = New SqlConnection("Initial Catalog=matrixVB;" & _
                   "Data Source=x5501;user id=sa;password=;")
           Dim strSQL As String = "SELECT Name, OT
    StartDate, MgrRemarks, OTCat  " & _
                         "FROM OT WHERE OTStartDate = '" & datetemp & "' AND Departcode = '" & dept & "'"
           cmd = New SqlCommand(strSQL, conn)
           conn.Open()


           Dim set1 As New DataSet()
           Dim SqlDataAdapter1 = New SqlDataAdapter(strSQL, conn)
           SqlDataAdapter1.Fill(set1, "OT")


           conn.Close()
           DataGrid1.DataSource = set1.Tables("Ot")
           DataGrid1.DataBind()


       End Sub


    'edit command event handler


       Public Sub DataGrid1_EditCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
         
           DataGrid1.EditItemIndex = e.Item.ItemIndex


           bind()
       End Sub


    'cancel command


       Public Sub DataGrid1_Cancel(ByVal Sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)


           DataGrid1.EditItemIndex = -1


           bind()
       End Sub


    'and the update command


       Public Sub DataGrid1_UpdateCommand(ByVal source As System.Object, ByVal e As DataGridCommandEventArgs)
           If (Page.IsValid) Then


               Dim txtrem As TextBox = e.Item.Cells(3).Controls(0)
               Dim txtcat As TextBox = e.Item.Cells(4).Controls(0)


               Dim UpdateCmd As String = "UPDATE OT SET MgrRemarks= '" & txtrem.Text & "' WHERE Name = ' " & e.Item.Cells(1).Text & "' "


               conn = New SqlConnection("Initial Catalog=matrixVB;" & _
    "Data Source=localhost;user id=sa;password=;")


               cmd = New SqlCommand(UpdateCmd, conn)


               conn.Open()


               cmd.ExecuteNonQuery()


               DataGrid1.EditItemIndex = -1


               bind()


           End If


       End Sub



    Name column is the first, followed by startdate, mgrremarks and otcat. what's wrong with this code?

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth