By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 2,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

Need help with Datagrid

P: n/a
Joe
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>
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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>

Nov 19 '05 #2

P: n/a
Make sure label2 and txtFirstname are up top in the generated code
section

Nov 19 '05 #3

P: n/a
Joe
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

Nov 19 '05 #4

P: n/a
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>

Nov 19 '05 #5

P: n/a
Joe
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>


Nov 19 '05 #6

P: n/a
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>
>
>


Nov 19 '05 #7

P: n/a
Joe
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>
>
>


Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.