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!!
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!!
Comment