Hi Everyone!
I have a ASP.NET webform that brings data in from SQL Server 2000 and displays it on a page. I want to update the webpage while in IE using the following code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<script language="VB" runat="server">
Dim MyConnection As SqlConnection
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
MyConnection = New SqlConnection("Data Source=SQLSERVER;Initial Catalog=upsizedCandidate;Integrated Security=True")
If Not (IsPostBack) Then
BindGrid()
End If
End Sub
Sub MyDataGrid_Edit(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
BindGrid()
End Sub
Sub MyDataGrid_Cancel(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub
Sub MyDataGrid_Update(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
Dim DS As DataSet
Dim MyCommand As SqlCommand
Dim UpdateCmd As String = "UPDATE ToddsTable1 SET myNewCol1 = @myNewCol1, myNewCol2 = @myNewCol2"
MyCommand = New SqlCommand(UpdateCmd, MyConnection)
MyCommand.Parameters.Add(New SqlParameter("@myNewCol1", SqlDbType.NVarChar, 40))
MyCommand.Parameters.Add(New SqlParameter("@myNewCol2", SqlDbType.NVarChar, 20))
MyCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
Dim Cols As String() = {"@myNewCol1", "@myNewCol2"}
Dim NumCols As Integer = E.Item.Cells.Count
Dim I As Integer
For I = 2 To NumCols - 2 'skip first, second and last column
Dim CurrentTextBox As TextBox
CurrentTextBox = E.Item.Cells(I).Controls(0)
Dim ColValue As String = CurrentTextBox.Text
MyCommand.Parameters(Cols(I - 1)).Value = Server.HtmlEncode(ColValue)
Next
MyCommand.Connection.Open()
Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Updated</b><br>" & UpdateCmd.ToString()
MyDataGrid.EditItemIndex = -1
Catch Exp As SQLException
If Exp.Number = 2627 Then
Message.InnerHtml = "ERROR: A record already exists with the same primary key"
Else
Message.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out"
End If
Message.Style("color") = "red"
End Try
MyCommand.Connection.Close()
BindGrid()
End Sub
Sub MyDataGrid_ItemDataBound(ByVal Sender As Object, ByVal E As DataGridItemEventArgs)
If (E.Item.ItemType = ListItemType.EditItem) Then
Dim i As Integer
For i = 0 To E.Item.Controls.Count - 1
If (E.Item.Controls(i).Controls(0).GetType().ToString() = "System.Web.UI.WebControls.TextBox") Then
Dim tb As TextBox
tb = E.Item.Controls(i).Controls(0)
tb.Text = Server.HtmlDecode(tb.Text)
End If
Next
End If
End Sub
Sub BindGrid()
Dim DS As DataSet
Dim MyCommand As SqlDataAdapter
MyCommand = New SqlDataAdapter("select * from ToddsTable1", MyConnection)
DS = New DataSet()
MyCommand.Fill(DS, "ToddsTable1")
MyDataGrid.DataSource = DS.Tables("ToddsTable1").DefaultView
MyDataGrid.DataBind()
End Sub
</script>
<body style="font: 10pt verdana">
<form id="Form1" runat="server">
<h3><font face="Verdana">Updating ToddsTable1</font></h3>
<span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p>
<ASP
ataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
DataKeyField="myNewCol1"
OnItemDataBound="MyDataGrid_ItemDataBound"
EnableViewState="false"
>
<Columns>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false"/>
</Columns>
</ASP
ataGrid>
</form>
</body>
</html>
The table is only 2 columns just to get something working but when I click the edit link my title still shows up but the table disappears. Any thoughts on what I could do differently? Thanks!
I have a ASP.NET webform that brings data in from SQL Server 2000 and displays it on a page. I want to update the webpage while in IE using the following code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<script language="VB" runat="server">
Dim MyConnection As SqlConnection
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
MyConnection = New SqlConnection("Data Source=SQLSERVER;Initial Catalog=upsizedCandidate;Integrated Security=True")
If Not (IsPostBack) Then
BindGrid()
End If
End Sub
Sub MyDataGrid_Edit(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
BindGrid()
End Sub
Sub MyDataGrid_Cancel(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub
Sub MyDataGrid_Update(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
Dim DS As DataSet
Dim MyCommand As SqlCommand
Dim UpdateCmd As String = "UPDATE ToddsTable1 SET myNewCol1 = @myNewCol1, myNewCol2 = @myNewCol2"
MyCommand = New SqlCommand(UpdateCmd, MyConnection)
MyCommand.Parameters.Add(New SqlParameter("@myNewCol1", SqlDbType.NVarChar, 40))
MyCommand.Parameters.Add(New SqlParameter("@myNewCol2", SqlDbType.NVarChar, 20))
MyCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
Dim Cols As String() = {"@myNewCol1", "@myNewCol2"}
Dim NumCols As Integer = E.Item.Cells.Count
Dim I As Integer
For I = 2 To NumCols - 2 'skip first, second and last column
Dim CurrentTextBox As TextBox
CurrentTextBox = E.Item.Cells(I).Controls(0)
Dim ColValue As String = CurrentTextBox.Text
MyCommand.Parameters(Cols(I - 1)).Value = Server.HtmlEncode(ColValue)
Next
MyCommand.Connection.Open()
Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Updated</b><br>" & UpdateCmd.ToString()
MyDataGrid.EditItemIndex = -1
Catch Exp As SQLException
If Exp.Number = 2627 Then
Message.InnerHtml = "ERROR: A record already exists with the same primary key"
Else
Message.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out"
End If
Message.Style("color") = "red"
End Try
MyCommand.Connection.Close()
BindGrid()
End Sub
Sub MyDataGrid_ItemDataBound(ByVal Sender As Object, ByVal E As DataGridItemEventArgs)
If (E.Item.ItemType = ListItemType.EditItem) Then
Dim i As Integer
For i = 0 To E.Item.Controls.Count - 1
If (E.Item.Controls(i).Controls(0).GetType().ToString() = "System.Web.UI.WebControls.TextBox") Then
Dim tb As TextBox
tb = E.Item.Controls(i).Controls(0)
tb.Text = Server.HtmlDecode(tb.Text)
End If
Next
End If
End Sub
Sub BindGrid()
Dim DS As DataSet
Dim MyCommand As SqlDataAdapter
MyCommand = New SqlDataAdapter("select * from ToddsTable1", MyConnection)
DS = New DataSet()
MyCommand.Fill(DS, "ToddsTable1")
MyDataGrid.DataSource = DS.Tables("ToddsTable1").DefaultView
MyDataGrid.DataBind()
End Sub
</script>
<body style="font: 10pt verdana">
<form id="Form1" runat="server">
<h3><font face="Verdana">Updating ToddsTable1</font></h3>
<span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p>
<ASP

Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
DataKeyField="myNewCol1"
OnItemDataBound="MyDataGrid_ItemDataBound"
EnableViewState="false"
>
<Columns>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false"/>
</Columns>
</ASP

</form>
</body>
</html>
The table is only 2 columns just to get something working but when I click the edit link my title still shows up but the table disappears. Any thoughts on what I could do differently? Thanks!