doubletalkin3
Programmer
Hi,
I'm trying to implement an update command on a datagrid, but on clicking update, nothing happens, and I can't see why. I've used strings instead of parameters in the sqlcommand and this works, and also passed straight strings into the parameters - this also works.
It looks like something is wrong with the converting to textbox lines in the update function.
I've tried lots of different things with my code and this is how it currently looks. Thanks in advance for any help -
---------------------------------------------
Sub BindGrid()
dAdap.Fill(dSet)
grdTelephoneList.DataSource = dSet
grdTelephoneList.DataKeyField = "RecordID"
grdTelephoneList.DataBind()
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
BindGrid()
End If
SqlConnection1.Open()
strSQLQuery = "SELECT RecordID, Forename, Surname, JobTitle, Extension, Mobile, Department FROM Contacts ORDER BY Surname"
dAdap = New SqlDataAdapter(strSQLQuery, SqlConnection1)
dSet = New DataSet
dAdap.Fill(dSet)
grdTelephoneList.DataSource = dSet
grdTelephoneList.DataKeyField = "RecordID"
grdTelephoneList.DataBind()
SqlConnection1.Close()
End Sub
Private Sub grdTelephoneList_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdTelephoneList.EditCommand
grdTelephoneList.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub
Private Sub grdTelephoneList_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdTelephoneList.CancelCommand
grdTelephoneList.EditItemIndex = -1
BindGrid()
End Sub
Private Sub grdTelephoneList_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdTelephoneList.UpdateCommand
Dim strForename As String = CType(e.Item.Cells(0).Controls(0), TextBox).Text
Dim strSurname As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
SqlCommand1.Parameters("@Forename").Value = strForename
SqlCommand1.Parameters("@Surname").Value = strSurname
SqlCommand1.Parameters("@RecordID").Value = grdTelephoneList.DataKeys(e.Item.ItemIndex)
SqlConnection1.Open()
SqlCommand1.ExecuteNonQuery()
grdTelephoneList.EditItemIndex = -1
BindGrid()
SqlConnection1.Close()
End Sub
I'm trying to implement an update command on a datagrid, but on clicking update, nothing happens, and I can't see why. I've used strings instead of parameters in the sqlcommand and this works, and also passed straight strings into the parameters - this also works.
It looks like something is wrong with the converting to textbox lines in the update function.
I've tried lots of different things with my code and this is how it currently looks. Thanks in advance for any help -
---------------------------------------------
Sub BindGrid()
dAdap.Fill(dSet)
grdTelephoneList.DataSource = dSet
grdTelephoneList.DataKeyField = "RecordID"
grdTelephoneList.DataBind()
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
BindGrid()
End If
SqlConnection1.Open()
strSQLQuery = "SELECT RecordID, Forename, Surname, JobTitle, Extension, Mobile, Department FROM Contacts ORDER BY Surname"
dAdap = New SqlDataAdapter(strSQLQuery, SqlConnection1)
dSet = New DataSet
dAdap.Fill(dSet)
grdTelephoneList.DataSource = dSet
grdTelephoneList.DataKeyField = "RecordID"
grdTelephoneList.DataBind()
SqlConnection1.Close()
End Sub
Private Sub grdTelephoneList_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdTelephoneList.EditCommand
grdTelephoneList.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub
Private Sub grdTelephoneList_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdTelephoneList.CancelCommand
grdTelephoneList.EditItemIndex = -1
BindGrid()
End Sub
Private Sub grdTelephoneList_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdTelephoneList.UpdateCommand
Dim strForename As String = CType(e.Item.Cells(0).Controls(0), TextBox).Text
Dim strSurname As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
SqlCommand1.Parameters("@Forename").Value = strForename
SqlCommand1.Parameters("@Surname").Value = strSurname
SqlCommand1.Parameters("@RecordID").Value = grdTelephoneList.DataKeys(e.Item.ItemIndex)
SqlConnection1.Open()
SqlCommand1.ExecuteNonQuery()
grdTelephoneList.EditItemIndex = -1
BindGrid()
SqlConnection1.Close()
End Sub