473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using datagrid in ASP.NET to get table from SQL SERVER-Not updating

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:


Expand|Select|Wrap|Line Numbers
  1. <%@ Import Namespace="System.Data" %>
  2. <%@ Import Namespace="System.Data.SqlClient" %>
  3. <%@ Import Namespace="System.Data.OleDb" %>
  4. <html>
  5. <script language="VB" runat="server">
  6. Dim MyConnection As SqlConnection
  7.  
  8. Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
  9.  
  10. MyConnection = New SqlConnection("Data Source=SQLSERVER;Initial Catalog=upsizedCandidate;Integrated Security=True")
  11.  
  12. If Not (IsPostBack) Then
  13. BindGrid()
  14. End If
  15.  
  16. End Sub
  17.  
  18. Sub MyDataGrid_Edit(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
  19.  
  20. MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
  21. BindGrid()
  22. End Sub
  23.  
  24. Sub MyDataGrid_Cancel(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
  25.  
  26. MyDataGrid.EditItemIndex = -1
  27. BindGrid()
  28. End Sub
  29.  
  30. Sub MyDataGrid_Update(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
  31.  
  32. Dim DS As DataSet
  33. Dim MyCommand As SqlCommand
  34.  
  35. Dim UpdateCmd As String = "UPDATE ToddsTable1 SET myNewCol1 = @myNewCol1, myNewCol2 = @myNewCol2"
  36.  
  37. MyCommand = New SqlCommand(UpdateCmd, MyConnection)
  38.  
  39. MyCommand.Parameters.Add(New SqlParameter("@myNewCol1", SqlDbType.NVarChar, 40))
  40. MyCommand.Parameters.Add(New SqlParameter("@myNewCol2", SqlDbType.NVarChar, 20))
  41.  
  42. MyCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
  43.  
  44. Dim Cols As String() = {"@myNewCol1", "@myNewCol2"}
  45.  
  46. Dim NumCols As Integer = E.Item.Cells.Count
  47.  
  48. Dim I As Integer
  49. For I = 2 To NumCols - 2 'skip first, second and last column
  50.  
  51. Dim CurrentTextBox As TextBox
  52. CurrentTextBox = E.Item.Cells(I).Controls(0)
  53. Dim ColValue As String = CurrentTextBox.Text
  54.  
  55. MyCommand.Parameters(Cols(I - 1)).Value = Server.HtmlEncode(ColValue)
  56. Next
  57.  
  58. MyCommand.Connection.Open()
  59.  
  60. Try
  61. MyCommand.ExecuteNonQuery()
  62. Message.InnerHtml = "<b>Record Updated</b><br>" & UpdateCmd.ToString()
  63. MyDataGrid.EditItemIndex = -1
  64. Catch Exp As SQLException
  65. If Exp.Number = 2627 Then
  66. Message.InnerHtml = "ERROR: A record already exists with the same primary key"
  67. Else
  68. Message.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out"
  69. End If
  70. Message.Style("color") = "red"
  71. End Try
  72.  
  73. MyCommand.Connection.Close()
  74.  
  75. BindGrid()
  76. End Sub
  77.  
  78.  
  79. Sub MyDataGrid_ItemDataBound(ByVal Sender As Object, ByVal E As DataGridItemEventArgs)
  80. If (E.Item.ItemType = ListItemType.EditItem) Then
  81. Dim i As Integer
  82. For i = 0 To E.Item.Controls.Count - 1
  83. If (E.Item.Controls(i).Controls(0).GetType().ToString() = "System.Web.UI.WebControls.TextBox") Then
  84. Dim tb As TextBox
  85. tb = E.Item.Controls(i).Controls(0)
  86. tb.Text = Server.HtmlDecode(tb.Text)
  87. End If
  88. Next
  89. End If
  90. End Sub
  91.  
  92. Sub BindGrid()
  93.  
  94. Dim DS As DataSet
  95. Dim MyCommand As SqlDataAdapter
  96. MyCommand = New SqlDataAdapter("select * from ToddsTable1", MyConnection)
  97.  
  98. DS = New DataSet()
  99. MyCommand.Fill(DS, "ToddsTable1")
  100.  
  101.  
  102. MyDataGrid.DataSource = DS.Tables("ToddsTable1").DefaultView
  103. MyDataGrid.DataBind()
  104.  
  105. End Sub
  106.  
  107. </script>
  108.  
  109.  
  110. <body style="font: 10pt verdana">
  111.  
  112. <form id="Form1" runat="server">
  113.  
  114. <h3><font face="Verdana">Updating ToddsTable1</font></h3>
  115.  
  116. <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p>
  117.  
  118.  
  119. <ASP:DataGrid id="MyDataGrid" runat="server"
  120. Width="700"
  121. BackColor="#ccccff"
  122. BorderColor="black"
  123. ShowFooter="false"
  124. CellPadding=3
  125. CellSpacing="0"
  126. Font-Name="Verdana"
  127. Font-Size="8pt"
  128. HeaderStyle-BackColor="#aaaadd"
  129. OnEditCommand="MyDataGrid_Edit"
  130. OnCancelCommand="MyDataGrid_Cancel"
  131. OnUpdateCommand="MyDataGrid_Update"
  132. DataKeyField="myNewCol1"
  133. OnItemDataBound="MyDataGrid_ItemDataBound"
  134. EnableViewState="false"
  135. >
  136.  
  137. <Columns>
  138. <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false"/>
  139. </Columns>
  140.  
  141. </ASP:DataGrid>
  142.  
  143. </form>
  144. </body>
  145. </html>
  146.  
  147.  
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. I've also tried to take out the IsPostBack statement from the Page_Load method and the text boxes show up for editting but when "update" is clicked the data returns back to it's original data. Any thoughts on what I could do differently? Anything will be appreciated just to help me think differently. Thanks!
Apr 12 '07 #1
2 4349
Ok, I switched my code as follows:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" %>
  2. <%@ Import Namespace="System.Data" %>
  3. <%@ Import Namespace="System.Data.SqlClient" %>
  4. <html>
  5. <head>
  6. <script runat="server">
  7. Public Sub Page_Load(Source As Object, E As EventArgs)
  8. If Not Page.IsPostBack Then
  9. BindData()
  10. End If
  11. End Sub
  12.  
  13. Public Sub DataGrid_Edit(ByVal Source As Object, _
  14. ByVal E As DataGridCommandEventArgs)
  15. myInfo.EditItemIndex = E.Item.ItemIndex
  16. BindData()
  17. End Sub
  18.  
  19. Public Sub DataGrid_Cancel(ByVal Source As Object, _
  20. ByVal E As DataGridCommandEventArgs)
  21. myInfo.EditItemIndex = -1
  22. BindData()
  23. End Sub
  24.  
  25. Public Sub DataGrid_Update(ByVal Source As Object, _
  26. ByVal E As DataGridCommandEventArgs)
  27. Dim myConnection As SqlConnection
  28. Dim myCommand As SqlCommand
  29. Dim txtmyNewCol1 As TextBox = E.Item.Cells(1).Controls(0)
  30. Dim txtmyNewCol2 As TextBox = E.Item.Cells(2).Controls(0)
  31. Dim strUpdateStmt As String
  32.  
  33. strUpdateStmt = "UPDATE ToddsTable1 SET " & _
  34. "myNewCol1 = '" & txtmyNewCol1.Text & "', " & _
  35. "myNewcol2 = '" & txtmyNewCol2.Text & "' "
  36.  
  37. myConnection = New SqlConnection( _
  38. "server=SQLSERVER;database=upsizedCandidate;Integrated Security=True;")
  39. myCommand = New SqlCommand(strUpdateStmt, myConnection)
  40. myConnection.Open()
  41. myCommand.ExecuteNonQuery()
  42.  
  43. myInfo.EditItemIndex = -1
  44. BindData()
  45. End Sub
  46.  
  47. Public Sub BindData()
  48. Dim myDataSet As New DataSet
  49. Dim mySqlDataAdapter As SqlDataAdapter
  50. mySqlDataAdapter = New SqlDataAdapter( _
  51. "SELECT * FROM ToddsTable1", _
  52. "server=SQLSERVER;database=upsizedCandidate;Integrated Security=True")
  53. mySqlDataAdapter.Fill(myDataSet, "ToddsTable1")
  54. myInfo.DataSource = myDataSet.Tables("ToddsTable1")
  55. myInfo.DataBind()
  56. End Sub
  57. </script>
  58. </head>
  59. <body>
  60. <form id="Form1" runat="server" method="post">
  61. <H3>Editing ToddsTable1 from SQL Server</H3>
  62.  
  63. <asp:DataGrid id="myInfo" runat="server"
  64. AutoGenerateColumns="False"
  65. OnEditCommand="DataGrid_Edit" Width="702px"
  66. OnCancelCommand="DataGrid_Cancel"
  67. OnUpdateCommand="DataGrid_Update">
  68.  
  69. <Columns>
  70. <asp:EditCommandColumn
  71. CancelText="Cancel"
  72. EditText="Edit"
  73. UpdateText="Update" />
  74. <asp:BoundColumn
  75. DataField="myNewCol1"
  76. HeaderText="myNewCol1" />
  77. <asp:BoundColumn
  78. DataField="myNewCol2"
  79. HeaderText="myNewCol2" />
  80. </Columns>
  81. </asp:DataGrid>
  82.  
  83.  
  84. </form>
  85. </body>
  86. </html>
  87.  
  88.  
Everything works fine except when I click"Edit" then edit the text and click "update" it changes every cell in the column to the value I entered in the edited column. Any thoughts on how I could fix this?
Apr 16 '07 #2
Hi,
I am Akash,
i am facing a problem while using datagrid control in vb.net, c#.net and ofcourse in vb and asp also while updating a data viewed in datagrid control at runtime by using buttons like edit, update or delete buttons. Can you plz send me proper used step by step code to edit or update a viewed data by using datagrid in all above language specially in vb.net and c#.net.


Ok, I switched my code as follows:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" %>
  2. <%@ Import Namespace="System.Data" %>
  3. <%@ Import Namespace="System.Data.SqlClient" %>
  4. <html>
  5. <head>
  6. <script runat="server">
  7. Public Sub Page_Load(Source As Object, E As EventArgs)
  8. If Not Page.IsPostBack Then
  9. BindData()
  10. End If
  11. End Sub
  12.  
  13. Public Sub DataGrid_Edit(ByVal Source As Object, _
  14. ByVal E As DataGridCommandEventArgs)
  15. myInfo.EditItemIndex = E.Item.ItemIndex
  16. BindData()
  17. End Sub
  18.  
  19. Public Sub DataGrid_Cancel(ByVal Source As Object, _
  20. ByVal E As DataGridCommandEventArgs)
  21. myInfo.EditItemIndex = -1
  22. BindData()
  23. End Sub
  24.  
  25. Public Sub DataGrid_Update(ByVal Source As Object, _
  26. ByVal E As DataGridCommandEventArgs)
  27. Dim myConnection As SqlConnection
  28. Dim myCommand As SqlCommand
  29. Dim txtmyNewCol1 As TextBox = E.Item.Cells(1).Controls(0)
  30. Dim txtmyNewCol2 As TextBox = E.Item.Cells(2).Controls(0)
  31. Dim strUpdateStmt As String
  32.  
  33. strUpdateStmt = "UPDATE ToddsTable1 SET " & _
  34. "myNewCol1 = '" & txtmyNewCol1.Text & "', " & _
  35. "myNewcol2 = '" & txtmyNewCol2.Text & "' "
  36.  
  37. myConnection = New SqlConnection( _
  38. "server=SQLSERVER;database=upsizedCandidate;Integrated Security=True;")
  39. myCommand = New SqlCommand(strUpdateStmt, myConnection)
  40. myConnection.Open()
  41. myCommand.ExecuteNonQuery()
  42.  
  43. myInfo.EditItemIndex = -1
  44. BindData()
  45. End Sub
  46.  
  47. Public Sub BindData()
  48. Dim myDataSet As New DataSet
  49. Dim mySqlDataAdapter As SqlDataAdapter
  50. mySqlDataAdapter = New SqlDataAdapter( _
  51. "SELECT * FROM ToddsTable1", _
  52. "server=SQLSERVER;database=upsizedCandidate;Integrated Security=True")
  53. mySqlDataAdapter.Fill(myDataSet, "ToddsTable1")
  54. myInfo.DataSource = myDataSet.Tables("ToddsTable1")
  55. myInfo.DataBind()
  56. End Sub
  57. </script>
  58. </head>
  59. <body>
  60. <form id="Form1" runat="server" method="post">
  61. <H3>Editing ToddsTable1 from SQL Server</H3>
  62.  
  63. <asp:DataGrid id="myInfo" runat="server"
  64. AutoGenerateColumns="False"
  65. OnEditCommand="DataGrid_Edit" Width="702px"
  66. OnCancelCommand="DataGrid_Cancel"
  67. OnUpdateCommand="DataGrid_Update">
  68.  
  69. <Columns>
  70. <asp:EditCommandColumn
  71. CancelText="Cancel"
  72. EditText="Edit"
  73. UpdateText="Update" />
  74. <asp:BoundColumn
  75. DataField="myNewCol1"
  76. HeaderText="myNewCol1" />
  77. <asp:BoundColumn
  78. DataField="myNewCol2"
  79. HeaderText="myNewCol2" />
  80. </Columns>
  81. </asp:DataGrid>
  82.  
  83.  
  84. </form>
  85. </body>
  86. </html>
  87.  
  88.  
Everything works fine except when I click"Edit" then edit the text and click "update" it changes every cell in the column to the value I entered in the edited column. Any thoughts on how I could fix this?
Jun 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stephen | last post by:
I have an the below datagrid which displays as follows: - ADDRESS Databound SELECT Databound SELECT Databound SELECT I need to to some sort of event...
3
by: Jeremy Ames | last post by:
I am trying to use a datagrid to pull information from a database. I prefer to use latebinding to this. Well, I finally got my information to populate the datagrid, but the information came out in...
0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
1
by: Jim Heavey | last post by:
I have a datagrid which is bound to data Table A. When I edit a row, I want 2 columns to show a DropDownList with on of those columns bound to Table B and the other bound to Table C. Can I do...
8
by: Inigo Jimenez | last post by:
I have an ASP .net web application installed in a Windows 2003 server. This web application has a webform that has a Datagrid. This Datagrid is filled with the data of a SQL table. I have a...
3
by: Rob Rogers | last post by:
I have an ASP.NET datagrid that loads an image. I would like to load one image if the value in my SQL table is true and another if it is false. Here is my code. <%@ Page Language="vb"...
2
by: Its_Me_SunnY | last post by:
Hi, can any one help me how to make a cell in data grid link to another page.... like i have a field "refno" in my table so when in get the values from table to datagrid the refno's of every...
6
by: p.mc | last post by:
Hi all, I'm having major problems with a userControl which contains a datagrid. My problem concerns data binding. The Page_Load() procedure calls the DataBind procedure to bind the datagrid...
5
by: tshad | last post by:
I have a datagrid that I cannot get to right justify a money amount (which is just a label). No matter what I do - it still right justifies it. <asp:TemplateColumn Visible="true"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.