Hi,
I’m new to asp.net. I want to create an asp.net page that allows user to
edit the data. I have pasted my code below. I am able to display the data in
a datagrid. At the bottom of the page there are two buttons “Save Changes”
and “Cancel”. When “Save Changes” button is clicked saveRec function is
called. But for some reason inside saveRec function the value of the textbox
is not visible. The line
Label2.Text = txtFirstName.Text
give an error -- System.NullReferenceException: Object reference not set to
an instance of an object.
Thanks for your help.
Joe
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!-- #include file=dsn.aspx -->
<script language="VB" runat="server" >
Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
' Save the referrer Url
ViewState("ReferrerUrl") = Request.UrlReferrer.ToString()
BindData()
End If
End Sub
Sub BindData()
dim strsql as string
'check for an id
If Request.QueryString.Item("id") Is Nothing Then
response.redirect("Resultslist.aspx")
Else
strsql="select * from [Results] where [ID]=" &
request.querystring("id")
End If
'Create a connection string
Dim connString as String
connString = getdsn()
'Open a connection
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()
'Create a command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
'Get a datareader
Dim objDataReader as OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)
'Do the DataBinding
myDataGrid.DataSource = objDataReader
myDataGrid.DataBind()
'Close the datareader/db connection
objDataReader.Close()
End Sub
Sub saveRec(Sender As Object, e As System.EventArgs)
'Dim txtFirstName As System.Web.UI.WebControls.TextBox
'Dim txtLastName As System.Web.UI.WebControls.TextBox
'Dim txtCity As System.Web.UI.WebControls.TextBox
dim strSQL as string
'check for an id
If Request.QueryString.Item("id") Is Nothing Then
response.redirect("Resultslist.aspx")
Else
strSQL="Update Results set first_name=@fn, last_name=@ln,
city=@ct where ID=" & request.querystring("id")
End If
'Create a connection string
Dim connString as String
connString = getdsn()
'Open a connection
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()
'Create a command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
objCommand.Parameters.Add("@fn", OleDbType.VarChar, 255)
objCommand.Parameters.Add("@ln", OleDbType.VarChar, 255)
objCommand.Parameters.Add("@ct", OleDbType.VarChar, 255)
objCommand.Parameters("@fn").Value = txtFirstName.Text
objCommand.Parameters("@ln").Value = txtLastName.Text
objCommand.Parameters("@ct").Value = txtCity.Text
Try
Label2.Text = txtFirstName.Text
' Execute the command
'objCommand.ExecuteNonQuery()
'myDataGrid.DataBind()
Catch Ex as Exception
Response.Write("<p><strong>An Error Occurred:</strong> " & Ex.ToString()
& "</p>" & vbCrLf)
Finally
objConnection.Close()
End Try
End Sub
Sub cancel(Sender As Object, e As EventArgs)
Response.Redirect(ViewState("ReferrerUrl").ToStrin g())
End Sub
</script>
<body>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="myDataGrid"
runat="server"
AutoGenerateColumns="False"
width="80%">
<ItemStyle Font-Size="X-Small"></ItemStyle>
<HeaderStyle Font-Size="X-Small" Font-Bold="True" ForeColor="White"
BackColor=""></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<b>Results detail</b>
</HeaderTemplate>
<ItemTemplate>
<table border="0" Cellpadding="4" Cellspacing="0" Width="80%"
style="FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, sans-serif">
<tr><td valign="top" unselectable="on">first_name</td>
<td><asp:TextBox id="txtFirstName" runat="server" Width="109px" Text='<%#
Container.DataItem("first_name")%>' /></td>
</tr>
<tr><td valign="top">last_name</td><td><asp:TextBox id="txtLastName"
runat="server" Width="109px" Text='<%# Container.DataItem("last_name")%>'
/></td></tr>
<tr><td valign="top">city</td><td><asp:TextBox id="txtCity" runat="server"
Width="109px" Text='<%# Container.DataItem("city")%>' /></td></tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<br />
<asp:Button ID="updateBtn" Runat="server" Text="Save Changes"
onclick="saveRec" />
<asp:Button ID="cancelBtn" Runat="server" Text="Cancel"
onclick="cancel" />
<asp:Label TEXT="" ID="Label2" Runat="Server" />
</form>
</center>
</body>
</html> 7 1769 http://www.wiley.com/WileyCDA/WileyA...peCd-NOTE.html
download chapter 16 at minimum. The pages are named according to function
and the code is all VB.NET.
---
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
"Joe" wrote: Hi,
I’m new to asp.net. I want to create an asp.net page that allows user to edit the data. I have pasted my code below. I am able to display the data in a datagrid. At the bottom of the page there are two buttons “Save Changes” and “Cancel”. When “Save Changes” button is clicked saveRec function is called. But for some reason inside saveRec function the value of the textbox is not visible. The line
Label2.Text = txtFirstName.Text
give an error -- System.NullReferenceException: Object reference not set to an instance of an object.
Thanks for your help.
Joe
<%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <!-- #include file=dsn.aspx -->
<script language="VB" runat="server" >
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then ' Save the referrer Url ViewState("ReferrerUrl") = Request.UrlReferrer.ToString() BindData() End If End Sub
Sub BindData() dim strsql as string 'check for an id If Request.QueryString.Item("id") Is Nothing Then response.redirect("Resultslist.aspx") Else strsql="select * from [Results] where [ID]=" & request.querystring("id") End If 'Create a connection string Dim connString as String connString = getdsn()
'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open()
'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConn ection) 'Do the DataBinding myDataGrid.DataSource = objDataReader myDataGrid.DataBind()
'Close the datareader/db connection objDataReader.Close() End Sub
Sub saveRec(Sender As Object, e As System.EventArgs) 'Dim txtFirstName As System.Web.UI.WebControls.TextBox 'Dim txtLastName As System.Web.UI.WebControls.TextBox 'Dim txtCity As System.Web.UI.WebControls.TextBox
dim strSQL as string 'check for an id If Request.QueryString.Item("id") Is Nothing Then response.redirect("Resultslist.aspx") Else strSQL="Update Results set first_name=@fn, last_name=@ln, city=@ct where ID=" & request.querystring("id") End If 'Create a connection string Dim connString as String connString = getdsn()
'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open()
'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection)
objCommand.Parameters.Add("@fn", OleDbType.VarChar, 255) objCommand.Parameters.Add("@ln", OleDbType.VarChar, 255) objCommand.Parameters.Add("@ct", OleDbType.VarChar, 255)
objCommand.Parameters("@fn").Value = txtFirstName.Text objCommand.Parameters("@ln").Value = txtLastName.Text objCommand.Parameters("@ct").Value = txtCity.Text
Try Label2.Text = txtFirstName.Text ' Execute the command 'objCommand.ExecuteNonQuery() 'myDataGrid.DataBind() Catch Ex as Exception Response.Write("<p><strong>An Error Occurred:</strong> " & Ex.ToString() & "</p>" & vbCrLf) Finally objConnection.Close()
End Try
End Sub
Sub cancel(Sender As Object, e As EventArgs) Response.Redirect(ViewState("ReferrerUrl").ToStrin g()) End Sub
</script> <body> <html> <head> <title>Untitled</title> </head> <body> <form id="Form1" method="post" runat="server"> <asp:datagrid id="myDataGrid" runat="server" AutoGenerateColumns="False" width="80%"> <ItemStyle Font-Size="X-Small"></ItemStyle> <HeaderStyle Font-Size="X-Small" Font-Bold="True" ForeColor="White" BackColor=""></HeaderStyle>
<Columns> <asp:TemplateColumn> <HeaderTemplate> <b>Results detail</b> </HeaderTemplate> <ItemTemplate> <table border="0" Cellpadding="4" Cellspacing="0" Width="80%" style="FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, sans-serif"> <tr><td valign="top" unselectable="on">first_name</td> <td><asp:TextBox id="txtFirstName" runat="server" Width="109px" Text='<%# Container.DataItem("first_name")%>' /></td> </tr> <tr><td valign="top">last_name</td><td><asp:TextBox id="txtLastName" runat="server" Width="109px" Text='<%# Container.DataItem("last_name")%>' /></td></tr> <tr><td valign="top">city</td><td><asp:TextBox id="txtCity" runat="server" Width="109px" Text='<%# Container.DataItem("city")%>' /></td></tr> </table> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid> <br /> <asp:Button ID="updateBtn" Runat="server" Text="Save Changes" onclick="saveRec" /> <asp:Button ID="cancelBtn" Runat="server" Text="Cancel" onclick="cancel" /> <asp:Label TEXT="" ID="Label2" Runat="Server" />
</form> </center> </body> </html>
Make sure label2 and txtFirstname are up top in the generated code
section
What do you mean by "up top in the generated code"?
Thanks,
Joe
"kl******@hotmail.com" wrote: Make sure label2 and txtFirstname are up top in the generated code section
This article is very good: http://aspnet.4guysfromrolla.com/art...71002-1.3.aspx
Alejandro.
"Joe" <Jo*@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.com... Hi,
I'm new to asp.net. I want to create an asp.net page that allows user to edit the data. I have pasted my code below. I am able to display the data in a datagrid. At the bottom of the page there are two buttons "Save Changes" and "Cancel". When "Save Changes" button is clicked saveRec function is called. But for some reason inside saveRec function the value of the textbox is not visible. The line
Label2.Text = txtFirstName.Text
give an error -- System.NullReferenceException: Object reference not set to an instance of an object.
Thanks for your help.
Joe
<%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <!-- #include file=dsn.aspx -->
<script language="VB" runat="server" >
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then ' Save the referrer Url ViewState("ReferrerUrl") = Request.UrlReferrer.ToString() BindData() End If End Sub
Sub BindData() dim strsql as string 'check for an id If Request.QueryString.Item("id") Is Nothing Then response.redirect("Resultslist.aspx") Else strsql="select * from [Results] where [ID]=" & request.querystring("id") End If 'Create a connection string Dim connString as String connString = getdsn()
'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open()
'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConn ection) 'Do the DataBinding myDataGrid.DataSource = objDataReader myDataGrid.DataBind()
'Close the datareader/db connection objDataReader.Close() End Sub
Sub saveRec(Sender As Object, e As System.EventArgs) 'Dim txtFirstName As System.Web.UI.WebControls.TextBox 'Dim txtLastName As System.Web.UI.WebControls.TextBox 'Dim txtCity As System.Web.UI.WebControls.TextBox
dim strSQL as string 'check for an id If Request.QueryString.Item("id") Is Nothing Then response.redirect("Resultslist.aspx") Else strSQL="Update Results set first_name=@fn, last_name=@ln, city=@ct where ID=" & request.querystring("id") End If 'Create a connection string Dim connString as String connString = getdsn()
'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open()
'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection)
objCommand.Parameters.Add("@fn", OleDbType.VarChar, 255) objCommand.Parameters.Add("@ln", OleDbType.VarChar, 255) objCommand.Parameters.Add("@ct", OleDbType.VarChar, 255)
objCommand.Parameters("@fn").Value = txtFirstName.Text objCommand.Parameters("@ln").Value = txtLastName.Text objCommand.Parameters("@ct").Value = txtCity.Text
Try Label2.Text = txtFirstName.Text ' Execute the command 'objCommand.ExecuteNonQuery() 'myDataGrid.DataBind() Catch Ex as Exception Response.Write("<p><strong>An Error Occurred:</strong> " & Ex.ToString() & "</p>" & vbCrLf) Finally objConnection.Close()
End Try
End Sub
Sub cancel(Sender As Object, e As EventArgs) Response.Redirect(ViewState("ReferrerUrl").ToStrin g()) End Sub
</script> <body> <html> <head> <title>Untitled</title> </head> <body> <form id="Form1" method="post" runat="server"> <asp:datagrid id="myDataGrid" runat="server" AutoGenerateColumns="False" width="80%"> <ItemStyle Font-Size="X-Small"></ItemStyle> <HeaderStyle Font-Size="X-Small" Font-Bold="True" ForeColor="White" BackColor=""></HeaderStyle>
<Columns> <asp:TemplateColumn> <HeaderTemplate> <b>Results detail</b> </HeaderTemplate> <ItemTemplate> <table border="0" Cellpadding="4" Cellspacing="0" Width="80%" style="FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, sans-serif"> <tr><td valign="top" unselectable="on">first_name</td> <td><asp:TextBox id="txtFirstName" runat="server" Width="109px" Text='<%# Container.DataItem("first_name")%>' /></td> </tr> <tr><td valign="top">last_name</td><td><asp:TextBox id="txtLastName" runat="server" Width="109px" Text='<%# Container.DataItem("last_name")%>' /></td></tr> <tr><td valign="top">city</td><td><asp:TextBox id="txtCity" runat="server" Width="109px" Text='<%# Container.DataItem("city")%>' /></td></tr> </table> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid> <br /> <asp:Button ID="updateBtn" Runat="server" Text="Save Changes" onclick="saveRec" /> <asp:Button ID="cancelBtn" Runat="server" Text="Cancel" onclick="cancel" /> <asp:Label TEXT="" ID="Label2" Runat="Server" />
</form> </center> </body> </html>
This article just like all other articles show only about inline editing in
DataGrid. Since my record has about 15 fields, in-line editing is not an
option. This is why I want to list field below one another. But I don't know
how to change the form layout in DataGrid.
Joe
"Alejandro Penate-Diaz" wrote: This article is very good: http://aspnet.4guysfromrolla.com/art...71002-1.3.aspx
Alejandro.
"Joe" <Jo*@discussions.microsoft.com> wrote in message news:DD**********************************@microsof t.com... Hi,
I'm new to asp.net. I want to create an asp.net page that allows user to edit the data. I have pasted my code below. I am able to display the data in a datagrid. At the bottom of the page there are two buttons "Save Changes" and "Cancel". When "Save Changes" button is clicked saveRec function is called. But for some reason inside saveRec function the value of the textbox is not visible. The line
Label2.Text = txtFirstName.Text
give an error -- System.NullReferenceException: Object reference not set to an instance of an object.
Thanks for your help.
Joe
<%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <!-- #include file=dsn.aspx -->
<script language="VB" runat="server" >
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then ' Save the referrer Url ViewState("ReferrerUrl") = Request.UrlReferrer.ToString() BindData() End If End Sub
Sub BindData() dim strsql as string 'check for an id If Request.QueryString.Item("id") Is Nothing Then response.redirect("Resultslist.aspx") Else strsql="select * from [Results] where [ID]=" & request.querystring("id") End If 'Create a connection string Dim connString as String connString = getdsn()
'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open()
'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConn ection) 'Do the DataBinding myDataGrid.DataSource = objDataReader myDataGrid.DataBind()
'Close the datareader/db connection objDataReader.Close() End Sub
Sub saveRec(Sender As Object, e As System.EventArgs) 'Dim txtFirstName As System.Web.UI.WebControls.TextBox 'Dim txtLastName As System.Web.UI.WebControls.TextBox 'Dim txtCity As System.Web.UI.WebControls.TextBox
dim strSQL as string 'check for an id If Request.QueryString.Item("id") Is Nothing Then response.redirect("Resultslist.aspx") Else strSQL="Update Results set first_name=@fn, last_name=@ln, city=@ct where ID=" & request.querystring("id") End If 'Create a connection string Dim connString as String connString = getdsn()
'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open()
'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection)
objCommand.Parameters.Add("@fn", OleDbType.VarChar, 255) objCommand.Parameters.Add("@ln", OleDbType.VarChar, 255) objCommand.Parameters.Add("@ct", OleDbType.VarChar, 255)
objCommand.Parameters("@fn").Value = txtFirstName.Text objCommand.Parameters("@ln").Value = txtLastName.Text objCommand.Parameters("@ct").Value = txtCity.Text
Try Label2.Text = txtFirstName.Text ' Execute the command 'objCommand.ExecuteNonQuery() 'myDataGrid.DataBind() Catch Ex as Exception Response.Write("<p><strong>An Error Occurred:</strong> " & Ex.ToString() & "</p>" & vbCrLf) Finally objConnection.Close()
End Try
End Sub
Sub cancel(Sender As Object, e As EventArgs) Response.Redirect(ViewState("ReferrerUrl").ToStrin g()) End Sub
</script> <body> <html> <head> <title>Untitled</title> </head> <body> <form id="Form1" method="post" runat="server"> <asp:datagrid id="myDataGrid" runat="server" AutoGenerateColumns="False" width="80%"> <ItemStyle Font-Size="X-Small"></ItemStyle> <HeaderStyle Font-Size="X-Small" Font-Bold="True" ForeColor="White" BackColor=""></HeaderStyle>
<Columns> <asp:TemplateColumn> <HeaderTemplate> <b>Results detail</b> </HeaderTemplate> <ItemTemplate> <table border="0" Cellpadding="4" Cellspacing="0" Width="80%" style="FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, sans-serif"> <tr><td valign="top" unselectable="on">first_name</td> <td><asp:TextBox id="txtFirstName" runat="server" Width="109px" Text='<%# Container.DataItem("first_name")%>' /></td> </tr> <tr><td valign="top">last_name</td><td><asp:TextBox id="txtLastName" runat="server" Width="109px" Text='<%# Container.DataItem("last_name")%>' /></td></tr> <tr><td valign="top">city</td><td><asp:TextBox id="txtCity" runat="server" Width="109px" Text='<%# Container.DataItem("city")%>' /></td></tr> </table> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid> <br /> <asp:Button ID="updateBtn" Runat="server" Text="Save Changes" onclick="saveRec" /> <asp:Button ID="cancelBtn" Runat="server" Text="Cancel" onclick="cancel" /> <asp:Label TEXT="" ID="Label2" Runat="Server" />
</form> </center> </body> </html>
Check out : http://msdn.microsoft.com/msdnmag/is...e/default.aspx
and http://www.dotnet247.com/247referenc...tonColumn.aspx
Juan T. Llibre
===========
"Joe" <Jo*@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com... This article just like all other articles show only about inline editing in DataGrid. Since my record has about 15 fields, in-line editing is not an option. This is why I want to list field below one another. But I don't know how to change the form layout in DataGrid.
Joe
"Alejandro Penate-Diaz" wrote:
This article is very good: http://aspnet.4guysfromrolla.com/art...71002-1.3.aspx
Alejandro.
"Joe" <Jo*@discussions.microsoft.com> wrote in message news:DD**********************************@microsof t.com... > Hi, > > I'm new to asp.net. I want to create an asp.net page that allows user > to > edit the data. I have pasted my code below. I am able to display the > data > in > a datagrid. At the bottom of the page there are two buttons "Save > Changes" > and "Cancel". When "Save Changes" button is clicked saveRec function is > called. But for some reason inside saveRec function the value of the > textbox > is not visible. The line > > Label2.Text = txtFirstName.Text > > give an error -- System.NullReferenceException: Object reference not > set > to > an instance of an object. > > Thanks for your help. > > Joe > > > <%@ Page Language="VB" Debug="true" %> > <%@ Import Namespace="System.Data" %> > <%@ Import Namespace="System.Data.OleDb" %> > <!-- #include file=dsn.aspx --> > > <script language="VB" runat="server" > > > Sub Page_Load(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Load > If Not Page.IsPostBack Then > ' Save the referrer Url > ViewState("ReferrerUrl") = Request.UrlReferrer.ToString() > BindData() > End If > End Sub > > Sub BindData() > dim strsql as string > 'check for an id > If Request.QueryString.Item("id") Is Nothing Then > response.redirect("Resultslist.aspx") > Else > strsql="select * from [Results] where [ID]=" & > request.querystring("id") > End If > 'Create a connection string > Dim connString as String > connString = getdsn() > > 'Open a connection > Dim objConnection as OleDbConnection > objConnection = New OleDbConnection(connString) > objConnection.Open() > > 'Create a command object > Dim objCommand as OleDbCommand > objCommand = New OleDbCommand(strSQL, objConnection) > 'Get a datareader > Dim objDataReader as OleDbDataReader > objDataReader = > objCommand.ExecuteReader(CommandBehavior.CloseConn ection) > 'Do the DataBinding > myDataGrid.DataSource = objDataReader > myDataGrid.DataBind() > > 'Close the datareader/db connection > objDataReader.Close() > End Sub > > Sub saveRec(Sender As Object, e As System.EventArgs) > 'Dim txtFirstName As System.Web.UI.WebControls.TextBox > 'Dim txtLastName As System.Web.UI.WebControls.TextBox > 'Dim txtCity As System.Web.UI.WebControls.TextBox > > dim strSQL as string > 'check for an id > If Request.QueryString.Item("id") Is Nothing Then > response.redirect("Resultslist.aspx") > Else > strSQL="Update Results set first_name=@fn, last_name=@ln, > city=@ct where ID=" & request.querystring("id") > End If > 'Create a connection string > Dim connString as String > connString = getdsn() > > 'Open a connection > Dim objConnection as OleDbConnection > objConnection = New OleDbConnection(connString) > objConnection.Open() > > 'Create a command object > Dim objCommand as OleDbCommand > objCommand = New OleDbCommand(strSQL, objConnection) > > objCommand.Parameters.Add("@fn", OleDbType.VarChar, 255) > objCommand.Parameters.Add("@ln", OleDbType.VarChar, 255) > objCommand.Parameters.Add("@ct", OleDbType.VarChar, 255) > > objCommand.Parameters("@fn").Value = txtFirstName.Text > objCommand.Parameters("@ln").Value = txtLastName.Text > objCommand.Parameters("@ct").Value = txtCity.Text > > Try > Label2.Text = txtFirstName.Text > ' Execute the command > 'objCommand.ExecuteNonQuery() > 'myDataGrid.DataBind() > Catch Ex as Exception > Response.Write("<p><strong>An Error Occurred:</strong> " & > Ex.ToString() > & "</p>" & vbCrLf) > Finally > objConnection.Close() > > End Try > > End Sub > > Sub cancel(Sender As Object, e As EventArgs) > Response.Redirect(ViewState("ReferrerUrl").ToStrin g()) > End Sub > > </script> > <body> > <html> > <head> > <title>Untitled</title> > </head> > <body> > <form id="Form1" method="post" runat="server"> > <asp:datagrid id="myDataGrid" > runat="server" > AutoGenerateColumns="False" > width="80%"> > <ItemStyle Font-Size="X-Small"></ItemStyle> > <HeaderStyle Font-Size="X-Small" Font-Bold="True" > ForeColor="White" > BackColor=""></HeaderStyle> > > <Columns> > <asp:TemplateColumn> > <HeaderTemplate> > <b>Results detail</b> > </HeaderTemplate> > <ItemTemplate> > <table border="0" Cellpadding="4" Cellspacing="0" > Width="80%" > style="FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, sans-serif"> > <tr><td valign="top" unselectable="on">first_name</td> > <td><asp:TextBox id="txtFirstName" runat="server" Width="109px" > Text='<%# > Container.DataItem("first_name")%>' /></td> > </tr> > <tr><td valign="top">last_name</td><td><asp:TextBox id="txtLastName" > runat="server" Width="109px" Text='<%# > Container.DataItem("last_name")%>' > /></td></tr> > <tr><td valign="top">city</td><td><asp:TextBox id="txtCity" > runat="server" > Width="109px" Text='<%# Container.DataItem("city")%>' /></td></tr> > </table> > </ItemTemplate> > </asp:TemplateColumn> > </Columns> > </asp:datagrid> > <br /> > <asp:Button ID="updateBtn" Runat="server" Text="Save Changes" > onclick="saveRec" /> > <asp:Button ID="cancelBtn" Runat="server" Text="Cancel" > onclick="cancel" /> > <asp:Label TEXT="" ID="Label2" Runat="Server" /> > > </form> > </center> > </body> > </html> > >
Thanks Juan. I went through the articles but I must be dumb because still I
cann't fi my problem. Damn this DataGrid. I am close to give up now.
Joe
"Juan T. Llibre [MVP]" wrote: Check out : http://msdn.microsoft.com/msdnmag/is...e/default.aspx and http://www.dotnet247.com/247referenc...tonColumn.aspx Juan T. Llibre =========== "Joe" <Jo*@discussions.microsoft.com> wrote in message news:2D**********************************@microsof t.com... This article just like all other articles show only about inline editing in DataGrid. Since my record has about 15 fields, in-line editing is not an option. This is why I want to list field below one another. But I don't know how to change the form layout in DataGrid.
Joe
"Alejandro Penate-Diaz" wrote:
This article is very good: http://aspnet.4guysfromrolla.com/art...71002-1.3.aspx
Alejandro.
"Joe" <Jo*@discussions.microsoft.com> wrote in message news:DD**********************************@microsof t.com... > Hi, > > I'm new to asp.net. I want to create an asp.net page that allows user > to > edit the data. I have pasted my code below. I am able to display the > data > in > a datagrid. At the bottom of the page there are two buttons "Save > Changes" > and "Cancel". When "Save Changes" button is clicked saveRec function is > called. But for some reason inside saveRec function the value of the > textbox > is not visible. The line > > Label2.Text = txtFirstName.Text > > give an error -- System.NullReferenceException: Object reference not > set > to > an instance of an object. > > Thanks for your help. > > Joe > > > <%@ Page Language="VB" Debug="true" %> > <%@ Import Namespace="System.Data" %> > <%@ Import Namespace="System.Data.OleDb" %> > <!-- #include file=dsn.aspx --> > > <script language="VB" runat="server" > > > Sub Page_Load(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Load > If Not Page.IsPostBack Then > ' Save the referrer Url > ViewState("ReferrerUrl") = Request.UrlReferrer.ToString() > BindData() > End If > End Sub > > Sub BindData() > dim strsql as string > 'check for an id > If Request.QueryString.Item("id") Is Nothing Then > response.redirect("Resultslist.aspx") > Else > strsql="select * from [Results] where [ID]=" & > request.querystring("id") > End If > 'Create a connection string > Dim connString as String > connString = getdsn() > > 'Open a connection > Dim objConnection as OleDbConnection > objConnection = New OleDbConnection(connString) > objConnection.Open() > > 'Create a command object > Dim objCommand as OleDbCommand > objCommand = New OleDbCommand(strSQL, objConnection) > 'Get a datareader > Dim objDataReader as OleDbDataReader > objDataReader = > objCommand.ExecuteReader(CommandBehavior.CloseConn ection) > 'Do the DataBinding > myDataGrid.DataSource = objDataReader > myDataGrid.DataBind() > > 'Close the datareader/db connection > objDataReader.Close() > End Sub > > Sub saveRec(Sender As Object, e As System.EventArgs) > 'Dim txtFirstName As System.Web.UI.WebControls.TextBox > 'Dim txtLastName As System.Web.UI.WebControls.TextBox > 'Dim txtCity As System.Web.UI.WebControls.TextBox > > dim strSQL as string > 'check for an id > If Request.QueryString.Item("id") Is Nothing Then > response.redirect("Resultslist.aspx") > Else > strSQL="Update Results set first_name=@fn, last_name=@ln, > city=@ct where ID=" & request.querystring("id") > End If > 'Create a connection string > Dim connString as String > connString = getdsn() > > 'Open a connection > Dim objConnection as OleDbConnection > objConnection = New OleDbConnection(connString) > objConnection.Open() > > 'Create a command object > Dim objCommand as OleDbCommand > objCommand = New OleDbCommand(strSQL, objConnection) > > objCommand.Parameters.Add("@fn", OleDbType.VarChar, 255) > objCommand.Parameters.Add("@ln", OleDbType.VarChar, 255) > objCommand.Parameters.Add("@ct", OleDbType.VarChar, 255) > > objCommand.Parameters("@fn").Value = txtFirstName.Text > objCommand.Parameters("@ln").Value = txtLastName.Text > objCommand.Parameters("@ct").Value = txtCity.Text > > Try > Label2.Text = txtFirstName.Text > ' Execute the command > 'objCommand.ExecuteNonQuery() > 'myDataGrid.DataBind() > Catch Ex as Exception > Response.Write("<p><strong>An Error Occurred:</strong> " & > Ex.ToString() > & "</p>" & vbCrLf) > Finally > objConnection.Close() > > End Try > > End Sub > > Sub cancel(Sender As Object, e As EventArgs) > Response.Redirect(ViewState("ReferrerUrl").ToStrin g()) > End Sub > > </script> > <body> > <html> > <head> > <title>Untitled</title> > </head> > <body> > <form id="Form1" method="post" runat="server"> > <asp:datagrid id="myDataGrid" > runat="server" > AutoGenerateColumns="False" > width="80%"> > <ItemStyle Font-Size="X-Small"></ItemStyle> > <HeaderStyle Font-Size="X-Small" Font-Bold="True" > ForeColor="White" > BackColor=""></HeaderStyle> > > <Columns> > <asp:TemplateColumn> > <HeaderTemplate> > <b>Results detail</b> > </HeaderTemplate> > <ItemTemplate> > <table border="0" Cellpadding="4" Cellspacing="0" > Width="80%" > style="FONT-SIZE: 11px; FONT-FAMILY: Verdana, Arial, sans-serif"> > <tr><td valign="top" unselectable="on">first_name</td> > <td><asp:TextBox id="txtFirstName" runat="server" Width="109px" > Text='<%# > Container.DataItem("first_name")%>' /></td> > </tr> > <tr><td valign="top">last_name</td><td><asp:TextBox id="txtLastName" > runat="server" Width="109px" Text='<%# > Container.DataItem("last_name")%>' > /></td></tr> > <tr><td valign="top">city</td><td><asp:TextBox id="txtCity" > runat="server" > Width="109px" Text='<%# Container.DataItem("city")%>' /></td></tr> > </table> > </ItemTemplate> > </asp:TemplateColumn> > </Columns> > </asp:datagrid> > <br /> > <asp:Button ID="updateBtn" Runat="server" Text="Save Changes" > onclick="saveRec" /> > <asp:Button ID="cancelBtn" Runat="server" Text="Cancel" > onclick="cancel" /> > <asp:Label TEXT="" ID="Label2" Runat="Server" /> > > </form> > </center> > </body> > </html> > > This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Junkguy |
last post by:
I need some help programmatically causing a row in a DataGrid to "flush"
its contents to its bound data (in Visual Studio 6 using Windows Forms
with C#).
My issue is I want to send an update to...
|
by: MrNobody |
last post by:
I am desperately in need of some help to get a summary row for my DataGrid. A
summary row is just a row always on the bottom which has totals for certain
columns.
I have been able to find...
|
by: ericm1155 |
last post by:
I am trying to display some information from a database in a form that
displays one record per line. When the user clicks anywhere on a line, that
record is highlighted and selected, and my program...
|
by: Luis E Valencia |
last post by:
I need a link on a datagrid, the link must have fields of the database
Like this
acciones.aspx?iddireccion=1&idindicador=4
Thanks
|
by: Jennyfer J Barco |
last post by:
Hello again I have a datagrid and I'm showing many records. I need 2 things:
I need that everytime they scroll down, the header stays lock so the user
can see it all the time. I need to scroll...
|
by: Anna |
last post by:
I have a simple DataGrid that displays a list of characteristics and
allows you to edit a description for each characteristic. The query
that feeds this involves a join, as the characteristics and...
|
by: Terry Olsen |
last post by:
I've got a datagrid set up to display data. I've also got an
Edit,Update,Cancel column set up to allow editing of data.
I've got a DropDownList (ID="ddl3")in the EditItemTemplate for a certain...
|
by: coleenholley |
last post by:
I've been trying since last Friday to get an answer on how to get a SPECIFIC row.cell value from a datagrid. I've had plenty of suggestions, but nothing works to get the value from a SPECIFIC Row...
|
by: Larry Woods |
last post by:
I have a datagrid that is carrying all fields of a record...except one. Now
I want to update the underlying database via a dataadapter. The update is
working but the field that is "left out" is...
|
by: Familjen Karlsson |
last post by:
Hi
I have downloaded some code and tried it and nothing happens with the
datagrid. Explain what is wrong and what I have to do please. I have tried
to Import the namespace Hamster and it didn't...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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: 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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
| |