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:DataGrid 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:DataGrid>
-
-
</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. 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!
2 4365
Ok, I switched my code as follows: -
<%@ Page Language="VB" %>
-
<%@ Import Namespace="System.Data" %>
-
<%@ Import Namespace="System.Data.SqlClient" %>
-
<html>
-
<head>
-
<script runat="server">
-
Public Sub Page_Load(Source As Object, E As EventArgs)
-
If Not Page.IsPostBack Then
-
BindData()
-
End If
-
End Sub
-
-
Public Sub DataGrid_Edit(ByVal Source As Object, _
-
ByVal E As DataGridCommandEventArgs)
-
myInfo.EditItemIndex = E.Item.ItemIndex
-
BindData()
-
End Sub
-
-
Public Sub DataGrid_Cancel(ByVal Source As Object, _
-
ByVal E As DataGridCommandEventArgs)
-
myInfo.EditItemIndex = -1
-
BindData()
-
End Sub
-
-
Public Sub DataGrid_Update(ByVal Source As Object, _
-
ByVal E As DataGridCommandEventArgs)
-
Dim myConnection As SqlConnection
-
Dim myCommand As SqlCommand
-
Dim txtmyNewCol1 As TextBox = E.Item.Cells(1).Controls(0)
-
Dim txtmyNewCol2 As TextBox = E.Item.Cells(2).Controls(0)
-
Dim strUpdateStmt As String
-
-
strUpdateStmt = "UPDATE ToddsTable1 SET " & _
-
"myNewCol1 = '" & txtmyNewCol1.Text & "', " & _
-
"myNewcol2 = '" & txtmyNewCol2.Text & "' "
-
-
myConnection = New SqlConnection( _
-
"server=SQLSERVER;database=upsizedCandidate;Integrated Security=True;")
-
myCommand = New SqlCommand(strUpdateStmt, myConnection)
-
myConnection.Open()
-
myCommand.ExecuteNonQuery()
-
-
myInfo.EditItemIndex = -1
-
BindData()
-
End Sub
-
-
Public Sub BindData()
-
Dim myDataSet As New DataSet
-
Dim mySqlDataAdapter As SqlDataAdapter
-
mySqlDataAdapter = New SqlDataAdapter( _
-
"SELECT * FROM ToddsTable1", _
-
"server=SQLSERVER;database=upsizedCandidate;Integrated Security=True")
-
mySqlDataAdapter.Fill(myDataSet, "ToddsTable1")
-
myInfo.DataSource = myDataSet.Tables("ToddsTable1")
-
myInfo.DataBind()
-
End Sub
-
</script>
-
</head>
-
<body>
-
<form id="Form1" runat="server" method="post">
-
<H3>Editing ToddsTable1 from SQL Server</H3>
-
-
<asp:DataGrid id="myInfo" runat="server"
-
AutoGenerateColumns="False"
-
OnEditCommand="DataGrid_Edit" Width="702px"
-
OnCancelCommand="DataGrid_Cancel"
-
OnUpdateCommand="DataGrid_Update">
-
-
<Columns>
-
<asp:EditCommandColumn
-
CancelText="Cancel"
-
EditText="Edit"
-
UpdateText="Update" />
-
<asp:BoundColumn
-
DataField="myNewCol1"
-
HeaderText="myNewCol1" />
-
<asp:BoundColumn
-
DataField="myNewCol2"
-
HeaderText="myNewCol2" />
-
</Columns>
-
</asp:DataGrid>
-
-
-
</form>
-
</body>
-
</html>
-
-
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?
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: -
<%@ Page Language="VB" %>
-
<%@ Import Namespace="System.Data" %>
-
<%@ Import Namespace="System.Data.SqlClient" %>
-
<html>
-
<head>
-
<script runat="server">
-
Public Sub Page_Load(Source As Object, E As EventArgs)
-
If Not Page.IsPostBack Then
-
BindData()
-
End If
-
End Sub
-
-
Public Sub DataGrid_Edit(ByVal Source As Object, _
-
ByVal E As DataGridCommandEventArgs)
-
myInfo.EditItemIndex = E.Item.ItemIndex
-
BindData()
-
End Sub
-
-
Public Sub DataGrid_Cancel(ByVal Source As Object, _
-
ByVal E As DataGridCommandEventArgs)
-
myInfo.EditItemIndex = -1
-
BindData()
-
End Sub
-
-
Public Sub DataGrid_Update(ByVal Source As Object, _
-
ByVal E As DataGridCommandEventArgs)
-
Dim myConnection As SqlConnection
-
Dim myCommand As SqlCommand
-
Dim txtmyNewCol1 As TextBox = E.Item.Cells(1).Controls(0)
-
Dim txtmyNewCol2 As TextBox = E.Item.Cells(2).Controls(0)
-
Dim strUpdateStmt As String
-
-
strUpdateStmt = "UPDATE ToddsTable1 SET " & _
-
"myNewCol1 = '" & txtmyNewCol1.Text & "', " & _
-
"myNewcol2 = '" & txtmyNewCol2.Text & "' "
-
-
myConnection = New SqlConnection( _
-
"server=SQLSERVER;database=upsizedCandidate;Integrated Security=True;")
-
myCommand = New SqlCommand(strUpdateStmt, myConnection)
-
myConnection.Open()
-
myCommand.ExecuteNonQuery()
-
-
myInfo.EditItemIndex = -1
-
BindData()
-
End Sub
-
-
Public Sub BindData()
-
Dim myDataSet As New DataSet
-
Dim mySqlDataAdapter As SqlDataAdapter
-
mySqlDataAdapter = New SqlDataAdapter( _
-
"SELECT * FROM ToddsTable1", _
-
"server=SQLSERVER;database=upsizedCandidate;Integrated Security=True")
-
mySqlDataAdapter.Fill(myDataSet, "ToddsTable1")
-
myInfo.DataSource = myDataSet.Tables("ToddsTable1")
-
myInfo.DataBind()
-
End Sub
-
</script>
-
</head>
-
<body>
-
<form id="Form1" runat="server" method="post">
-
<H3>Editing ToddsTable1 from SQL Server</H3>
-
-
<asp:DataGrid id="myInfo" runat="server"
-
AutoGenerateColumns="False"
-
OnEditCommand="DataGrid_Edit" Width="702px"
-
OnCancelCommand="DataGrid_Cancel"
-
OnUpdateCommand="DataGrid_Update">
-
-
<Columns>
-
<asp:EditCommandColumn
-
CancelText="Cancel"
-
EditText="Edit"
-
UpdateText="Update" />
-
<asp:BoundColumn
-
DataField="myNewCol1"
-
HeaderText="myNewCol1" />
-
<asp:BoundColumn
-
DataField="myNewCol2"
-
HeaderText="myNewCol2" />
-
</Columns>
-
</asp:DataGrid>
-
-
-
</form>
-
</body>
-
</html>
-
-
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?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 which takes the value from the databound
cell and displays it in a asp table when the select(linkbutton) is clicked.
Id also like to add the value in the databound cell into an array. Could
|
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 a table format. Is there a way
that I can make the datagrid use list box or some other kind of grid?
Perhaps I am using the wrong components. Someone please help.
|
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 added row it also removes the row at the end along with the added row.
Plz tell me if, I am missing any thing.
Code
</asp:datagrid>
|
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 that?
|
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 button that inserts a new row in the SQL table and then
refresh the datagrid.
| |
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" AutoEventWireup="false"
Codebehind="Search.aspx.vb" Inherits="PROJECT.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
|
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 record must link to
another page..
thanks in advance...
SUNNY
|
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 to a DataSet.
If i include an if statement to prevent the data binding from occuring
on a page PostBack in the following way:
|
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" itemStyle-Width="100px"
HeaderText="Amount Per Job" HeaderStyle-Font-Bold="true"
ItemStyle-VerticalAlign="middle" ItemStyle-HorizontalAlign="right" >
<itemtemplate>
<asp:Label ID="JobBoardPriceDisplay" style="text-align:right"
runat="server"/>
</itemtemplate>
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |