Saving using datagrid in VB.NET from SQL Server by SAVE button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • toddw607
    New Member
    • Feb 2007
    • 41

    #1

    Saving using datagrid in VB.NET from SQL Server by SAVE button

    Hi all!
    I am attempting to bring an SQL Server table into ASP.NET using the datagrid . I have set all cells to be a text box by which the user can just click on and edit the cell. I have a save button and when I click the save button the page reloads and the new data is in the cell, however when I close out and come back in the data is no longer there. I have a databind() subroutine which includes a postback included within my code for the save button which is as follows:
    Private Sub doSave(ByVal sender As Object, ByVal e As System.EventArg s)
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim mySqlDataAdapte r As SqlDataAdapter
    Dim dstCopy As New DataSet
    Dim strQuery As String

    Dim intError As Integer = 0

    myConnection = New SqlConnection( _
    "server=SQLSERV ER;database=ups izedCandidate;I ntegrated Security=True;" )
    myConnection.Op en()
    Dim dgItem As DataGridItem
    For Each dgItem In myInfo.Items
    'Dim txtID As TextBox = _
    ' CType(dgItem.Ce lls(0).FindCont rol("txtID"), TextBox)
    Dim txtCAGE As TextBox = _
    CType(dgItem.Ce lls(1).FindCont rol("txtCAGE"), TextBox)
    Dim txtName As TextBox = _
    CType(dgItem.Ce lls(2).FindCont rol("txtName"), TextBox)
    Dim txtStreet As TextBox = _
    CType(dgItem.Ce lls(3).FindCont rol("txtStreet" ), TextBox)
    Dim txtCity As TextBox = _
    CType(dgItem.Ce lls(4).FindCont rol("txtCity"), TextBox)
    Dim txtState As TextBox = _
    CType(dgItem.Ce lls(5).FindCont rol("txtState") , TextBox)
    Dim txtNation As TextBox = _
    CType(dgItem.Ce lls(6).FindCont rol("txtNation" ), TextBox)
    Dim txtPostal As TextBox = _
    CType(dgItem.Ce lls(7).FindCont rol("txtPostal" ), TextBox)
    Dim txtPhone As TextBox = _
    CType(dgItem.Ce lls(8).FindCont rol("txtPhone") , TextBox)

    Dim item As Integer
    If txtCAGE.Text.Tr im <> String.Empty Then
    If IsNumeric(dgIte m.Cells(9).Text ) Then
    item = CInt(dgItem.Cel ls(9).Text)
    strQuery = "Update Table1 SET CAGE='" + txtCAGE.Text _
    + "',Name='" + txtName.Text + _
    +"',Street=' " + txtStreet.Text + _
    +"',City='" + txtCity.Text + _
    +"',State='" + txtState.Text + _
    +"',Nation=' " + txtNation.Text + _
    +"',Postal=' " + txtPostal.Text + _
    +"',Phone='" + txtPhone.Text + _
    "' WHERE ID=" + CStr(item)
    Else
    item = 0
    strQuery = "INSERT INTO XH(CAGE,Name,St reet,City,State ,Nation,Postal, Phone) VALUES('" _
    + txtCAGE.Text + "','" + _
    +txtName.Text + "','" + _
    +txtStreet.Text + "','" + _
    +txtCity.Text + "','" + _
    +txtState.Text + "','" + _
    +txtNation.Text + "','" + _
    +txtPostal.Text + "','" + _
    +txtPhone.Text + "','" + "')"
    End If
    myCommand = New SqlCommand(strQ uery, myConnection)
    myCommand.Execu teNonQuery()
    strQuery = String.Empty
    myCommand.Dispo se()
    End If
    Next

    myConnection.Cl ose()
    BindData()
    End Sub

    Here is my page_load and databind:
    Sub Page_Load(ByVal sender As Object, ByVal E As EventArgs)
    If Not Page.IsPostBack Then
    BindData()
    End If
    End Sub


    Public Sub BindData()
    Dim myDataSet As New DataSet
    Dim mySqlDataAdapte r As SqlDataAdapter
    mySqlDataAdapte r = New SqlDataAdapter( _
    "SELECT * FROM XH", _
    "server=SQLSERV ER;database=ups izedCandidate;I ntegrated Security=True")
    mySqlDataAdapte r.Fill(myDataSe t, "XH")
    myInfo.DataSour ce = myDataSet.Table s("XH")
    myInfo.DataBind ()
    End Sub

    My datagrid is:
    <asp:DataGrid id="myInfo"
    OnItemCommand=" doInsert"
    OnEditCommand=" doEdit"
    OnCancelCommand ="doCancel"
    OnUpdateCommand ="doUpdate"
    OnSaveCommand=" doSave"
    <asp:TemplateCo lumn HeaderText="Sav e">
    <FooterTemplate >
    <asp:Button CommandName="Sa ve" Text="Save" ID="btnSave" runat="server" />
    </FooterTemplate>
    </asp:TemplateCol umn>


    Anyone have any thoughts or suggestions, it would be GREATLY appreciated. Thanks in advance!!
  • toddw607
    New Member
    • Feb 2007
    • 41

    #2
    My first problem was that I didn't properly call my doSave function. I just put a new button inside my page but outside the datagrid (above the datagrid code) and when I attempt to save it directs to the function but now I get a crazy error message that looks like:


    Invalid postback or callback argument. Event validation is enabled using <pages enableEventVali dation="true"/> in configuration or <%@ Page EnableEventVali dation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptMan ager.RegisterFo rEventValidatio n method in order to register the postback or callback data for validation.

    Has anyone else got this error before. If anyone could help me out your help will be greatly appreciated. Thanks for helping

    Comment

    • toddw607
      New Member
      • Feb 2007
      • 41

      #3
      Ok, I placed a different button inside the datagrid to access the save method and now I'm getting a much simpler error. I just wanted to post it so nobody wastes any time trying to figure out the last error I posted. The error I'm getting now is:
      Input string was not in a correct format.
      I've tested it numerous times and this error is displayed when I click on the save button and it's coming straight out of my method. Any thoughts or suggestions would help me very much.

      Comment

      Working...