Need help with Datagrid

G

Guest

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.CloseConnection)
'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").ToString())
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>
 
G

Guest

http://www.wiley.com/WileyCDA/WileyAncillary/productCd-0764548492,typeCd-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 said:
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.CloseConnection)
'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").ToString())
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>
 
G

Guest

Can you tell me what and where I am making a mistake in my code?

Thanks for the pointer.

Joe



Cowboy (Gregory A. Beamer) - MVP said:
http://www.wiley.com/WileyCDA/WileyAncillary/productCd-0764548492,typeCd-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 said:
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.CloseConnection)
'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").ToString())
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>
 
A

Alejandro Penate-Diaz

This article is very good:
http://aspnet.4guysfromrolla.com/articles/071002-1.3.aspx

Alejandro.




Joe said:
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.CloseConnection)
'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").ToString())
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>
 
G

Guest

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 said:
This article is very good:
http://aspnet.4guysfromrolla.com/articles/071002-1.3.aspx

Alejandro.




Joe said:
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.CloseConnection)
'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").ToString())
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>
 
J

Juan T. Llibre [MVP]

Check out :
http://msdn.microsoft.com/msdnmag/issues/04/01/CuttingEdge/default.aspx
and
http://www.dotnet247.com/247reference/System/Web/UI/WebControls/ButtonColumn.aspx



Juan T. Llibre
===========
Joe said:
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 said:
This article is very good:
http://aspnet.4guysfromrolla.com/articles/071002-1.3.aspx

Alejandro.




Joe said:
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.CloseConnection)
'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").ToString())
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>
 
G

Guest

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 said:
Check out :
http://msdn.microsoft.com/msdnmag/issues/04/01/CuttingEdge/default.aspx
and
http://www.dotnet247.com/247reference/System/Web/UI/WebControls/ButtonColumn.aspx



Juan T. Llibre
===========
Joe said:
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 said:
This article is very good:
http://aspnet.4guysfromrolla.com/articles/071002-1.3.aspx

Alejandro.




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.CloseConnection)
'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").ToString())
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top