473,410 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Need help with Datagrid

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
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>

Nov 19 '05 #2
Make sure label2 and txtFirstname are up top in the generated code
section

Nov 19 '05 #3
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
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...
0
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...
1
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...
5
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
1
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...
1
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...
10
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...
21
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...
3
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...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
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...
0
agi2029
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,...

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

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