9 Replies - 2931 Views - Last Post: 17 December 2009 - 10:59 AM Rate Topic: -----

#1 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Having problems when editing a gridview

Posted 10 December 2009 - 11:36 AM

Please help. I have a gridview with a one template field column. The template field has image and label in it. When I added the Edit, Update, Cancel field, I'm having error when click on the Edit link. Here's the code and the error message:
  Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
		If e.Row.RowType = DataControlRowType.DataRow Then
			Dim lbl As Label = DirectCast(e.Row.FindControl("Label1"), Label)
			Dim img As Image = DirectCast(e.Row.FindControl("Image1"), Image)

			If lbl.Text = "5" Then
				img.ImageUrl = "~/pics/rating_5.gif" 'Here I receive this error message: "Object reference not set to an instance of an object."
				lbl.Text = "Very safe"
			End If

		End If
	End Sub


When I comment out that line, I was able to edit the row but there's no image. I'm also not sure how to set the template field a read-only. Please help. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Having problems when editing a gridview

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Having problems when editing a gridview

Posted 10 December 2009 - 04:51 PM

You are getting the error because it can't find the control and the object is Null.

You need to narrow it down further than just the Row. You need to find the control in the specific Cell that contains it.

Post the source for the page so I can see the columns you have declared in your GridView.
Was This Post Helpful? 0
  • +
  • -

#3 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Re: Having problems when editing a gridview

Posted 10 December 2009 - 07:55 PM

View PostJayman, on 10 Dec, 2009 - 03:51 PM, said:

You are getting the error because it can't find the control and the object is Null.

You need to narrow it down further than just the Row. You need to find the control in the specific Cell that contains it.

Post the source for the page so I can see the columns you have declared in your GridView.


Sorry about that...I forgot...here it is:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="hijackthis._Default" MasterPageFile="~/Site1.Master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
		
	<center><asp:FileUpload ID="FileUpload1" runat="server" /></center>
	<br />
	<center><asp:TextBox ID="TextBox1" runat="server" Height="146px" Width="404px"></asp:TextBox></center>
	<br />
	<center><asp:Button ID="btnAnalyze" runat="server" Text="Analyze" />
		<br />
	</center>
	<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
		GridLines="None" AutoGenerateColumns="False" DataKeyNames="entry" 
		DataSourceID="SqlDataSource1">
		<RowStyle BackColor="#E3EAEB" />
		<Columns>
			<asp:BoundField DataField="entry" HeaderText="Entry" SortExpression="entry" 
				ReadOnly="True" />
			<asp:TemplateField HeaderText="Kind" SortExpression="kind">
				<EditItemTemplate>
					<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("kind") %>'></asp:TextBox>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Image ID="Image2" runat="server" />
					<asp:Label ID="Label2" runat="server" Text='<%# Bind("kind") %>' 
						Visible="False"></asp:Label>
				</ItemTemplate>
			</asp:TemplateField>
			<asp:TemplateField HeaderText="Rating" SortExpression="Expr1">
				<EditItemTemplate>
					<asp:Label ID="Label1" runat="server" Text='<%# Eval("Expr1") %>'></asp:Label>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Image ID="Image1" runat="server" Height="10px" Width="50px" />
					<br />
					<asp:Label ID="Label1" runat="server" Text='<%# Bind("Expr1") %>'></asp:Label>
				</ItemTemplate>
			</asp:TemplateField>
			<asp:BoundField DataField="info" HeaderText="Info" SortExpression="info" />
			<asp:CommandField ShowEditButton="True" />
		</Columns>
		<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
		<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
		<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
		<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
		<EditRowStyle BackColor="#7C6F57" />
		<AlternatingRowStyle BackColor="White" />
	</asp:GridView>
			<br />
			<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
		ConnectionString="<%$ ConnectionStrings:tryConnectionString %>" 
		
		
		SelectCommand="SELECT tb_hijack.entry, tb_hijack.info, (SELECT rating FROM tb_comments WHERE (tb_hijack.entry = entry)) AS Expr1, tb_hijack.kind FROM tb_hijack INNER JOIN tb_comments AS tb_comments_1 ON tb_hijack.entry = tb_comments_1.entry">
	</asp:SqlDataSource>
			<br />
	<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
			<td width="26"> 
			</td> 
			<td width="26"></td> 
		  &nbsp;</tr> 
	</asp:Table>
		
</asp:Content>



I'm not sure why it's having error because I want those template fields to be read only ones. That also the reason why I'm looking for a way to set the column to read only.
Was This Post Helpful? 0
  • +
  • -

#4 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Re: Having problems when editing a gridview

Posted 11 December 2009 - 10:39 AM

I'm having problem finding the web control inside a template field. That template field is also inside another template field of a gridview.

Here's the html source:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="hijackthis._Default" MasterPageFile="~/Site1.Master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
		
	<center><asp:FileUpload ID="FileUpload1" runat="server" /></center>
	<br />
	<center><asp:TextBox ID="TextBox1" runat="server" Height="146px" Width="404px"></asp:TextBox></center>
	<br />
	<center><asp:Button ID="btnAnalyze" runat="server" Text="Analyze" />
		<br />
		<br />
	</center>
	<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
		GridLines="None" AutoGenerateColumns="False" DataKeyNames="entry" 
		DataSourceID="SqlDataSource1">
		<RowStyle BackColor="#E3EAEB" />
		<Columns>
			<asp:TemplateField HeaderText="Entry" SortExpression="entry">
				<EditItemTemplate>
					<asp:Label ID="Label2" runat="server" Text='<%# Eval("entry") %>'></asp:Label>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Label ID="Label3" runat="server" Text='<%# Bind("entry") %>'></asp:Label>
					<br />
					<br />
					<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
						DataSourceID="SqlDataSource1" ShowHeader="False">
						<Columns>
							<asp:TemplateField HeaderText="rating" SortExpression="rating">
								<EditItemTemplate>
									<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("rating") %>'></asp:TextBox>
								</EditItemTemplate>
								<ItemTemplate>
									<asp:Image ID="Image3" runat="server" />
									<asp:Label ID="Label1" runat="server" Text='<%# Bind("rating") %>' 
										Visible="False"></asp:Label>
								</ItemTemplate>
							</asp:TemplateField>
							<asp:BoundField DataField="comment" HeaderText="comment" 
								SortExpression="comment" />
							<asp:BoundField DataField="whois" HeaderText="whois" SortExpression="whois" />
						</Columns>
					</asp:GridView>
					<br />
					<asp:Label ID="Label4" runat="server" Text="Visitor's Assessment"></asp:Label>
					<br />
					<asp:Label ID="Label5" runat="server" Text="Comment:"></asp:Label>
					<br />
					<asp:TextBox ID="TextBox2" runat="server" Height="86px" Width="224px"></asp:TextBox>
					<br />
					<br />
					<asp:Label ID="Label6" runat="server" Text="Rating"></asp:Label>
					<br />
					<asp:DropDownList ID="DropDownList1" runat="server">
						<asp:ListItem>1 - Extremely Nasty</asp:ListItem>
						<asp:ListItem>2 - Nasty</asp:ListItem>
						<asp:ListItem Selected="True">3 - Neutral</asp:ListItem>
						<asp:ListItem>4 - Safe</asp:ListItem>
						<asp:ListItem>5 - Very Safe</asp:ListItem>
					</asp:DropDownList>
					<br />
					<br />
					<asp:Button ID="btnComment" runat="server" Text="Submit" />
					<br />
					<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
						ConnectionString="<%$ ConnectionStrings:tryConnectionString %>" 
						SelectCommand="SELECT tb_comments.comment, tb_comments.rating, tb_comments.whois FROM tb_comments INNER JOIN tb_hijack ON tb_comments.entry = tb_hijack.entry">
					</asp:SqlDataSource>
				</ItemTemplate>
			</asp:TemplateField>
			<asp:TemplateField HeaderText="Kind" SortExpression="kind">
				<EditItemTemplate>
					<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("kind") %>'></asp:TextBox>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Image ID="Image2" runat="server" />
					<asp:Label ID="Label2" runat="server" Text='<%# Bind("kind") %>' 
						Visible="False"></asp:Label>
				</ItemTemplate>
				<ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
			</asp:TemplateField>
			<asp:TemplateField HeaderText="Rating" SortExpression="Expr1">
				<EditItemTemplate>
					<asp:Label ID="Label1" runat="server" Text='<%# Eval("Expr1") %>'></asp:Label>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Image ID="Image1" runat="server" Height="10px" Width="50px" />
					<br />
					<asp:Label ID="Label1" runat="server" Text='<%# Bind("Expr1") %>'></asp:Label>
				</ItemTemplate>
				<ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
			</asp:TemplateField>
			<asp:BoundField DataField="info" HeaderText="Info" SortExpression="info" >
			<ItemStyle VerticalAlign="Top" />
			</asp:BoundField>
			<asp:CommandField ShowEditButton="True" >
			<ItemStyle VerticalAlign="Top" />
			</asp:CommandField>
		</Columns>
		<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
		<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
		<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
		<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
		<EditRowStyle BackColor="#7C6F57" />
		<AlternatingRowStyle BackColor="White" />
	</asp:GridView>
			<br />
			<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
		ConnectionString="<%$ ConnectionStrings:tryConnectionString %>" 
		
		
		SelectCommand="SELECT tb_hijack.entry, tb_hijack.info, (SELECT rating FROM tb_comments WHERE (tb_hijack.entry = entry)) AS Expr1, tb_hijack.kind FROM tb_hijack INNER JOIN tb_comments AS tb_comments_1 ON tb_hijack.entry = tb_comments_1.entry">
	</asp:SqlDataSource>
			<br />
	<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
			<td width="26"> 
			</td> 
			<td width="26"></td> 
		  &nbsp;</tr> 
	</asp:Table>
		
</asp:Content>



Here's the rowdatabound code:
		If e.Row.RowType = DataControlRowType.DataRow Then

			Dim lbl As Label = DirectCast(e.Row.FindControl("Label1"), Label)
			Dim lbl2 As Label = DirectCast(e.Row.FindControl("Label2"), Label)
			Dim img As Image = DirectCast(e.Row.FindControl("Image1"), Image)
			Dim img2 As Image = DirectCast(e.Row.FindControl("Image2"), Image)
			Dim img3 As Image = DirectCast(e.Row.FindControl("Image3"), Image)


			If lbl.Text = "5" Then
				img.ImageUrl = "~/pics/rating_5.gif"
				img.ToolTip = lbl.Text
				lbl.Text = "Very safe"
			End If

			img2.ImageUrl = "~/pics/" & lbl2.Text & ".gif"
			img2.ToolTip = lbl2.Text.ToUpper
			img3.ImageUrl = "~/pics/" & lbl2.Text & ".gif"
			img3.ToolTip = lbl2.Text.ToUpper
		End If



I'm not sure if I need to continue my last post "Having problems when editing a gridview" since that one, I'm having problem when I click the Edit link. But they're of the same project. Please do help. Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Having problems when editing a gridview

Posted 11 December 2009 - 12:50 PM

Topics merged. Lets keep this all together since it is all related. Thanks.

Based on the source you posted, try changing the following 2 lines and see if it now finds the controls.

Dim lbl As Label = DirectCast(e.Row.Cells(2).FindControl("Label1"), Label)
Dim img As Image = DirectCast(e.Row.Cells(2).FindControl("Image1"), Image)

Was This Post Helpful? 0
  • +
  • -

#6 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Re: Having problems when editing a gridview

Posted 11 December 2009 - 08:16 PM

Here's what I did to get the control:
			Dim gv As GridView = DirectCast(e.Row.FindControl("GridView2"), GridView)
			Dim lbl3 As Label = DirectCast(gv.Rows.Item(0).FindControl("Label7"), Label)
			Dim img3 As Image = DirectCast(gv.Rows.Item(0).FindControl("Image3"), Image)



The problem is, it just get the control of a particular row, not all of them. Also there's still the problem of clicking the Edit link and it's still not finding the control.

Just to give you a better idea of what I'm doing. I'm trying to create another www.hijackthis.de just to improve my NOOB skills in asp.net. I don't have any intention of creating one externally. The reason I chose this is that this was the first time I saw a template field inside a template field so I want to try it out. :)
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Having problems when editing a gridview

Posted 12 December 2009 - 10:30 AM

It is not going to find an Image control when you click the Edit link, because you have not defined an Image in the EditItemTemplate.

Secondly, you only have a reference to a single row using 'e'. If you want to get the control in each of the rows then you will need to loop through all the rows getting each control, even in a nested GridView.

However, only one row will be in Edit mode. So I am a little unclear what you are trying to accomplish.

Can you elaborate more as to the desired result?
Was This Post Helpful? 0
  • +
  • -

#8 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Re: Having problems when editing a gridview

Posted 12 December 2009 - 12:02 PM

View PostJayman, on 12 Dec, 2009 - 09:30 AM, said:

It is not going to find an Image control when you click the Edit link, because you have not defined an Image in the EditItemTemplate.

Secondly, you only have a reference to a single row using 'e'. If you want to get the control in each of the rows then you will need to loop through all the rows getting each control, even in a nested GridView.

However, only one row will be in Edit mode. So I am a little unclear what you are trying to accomplish.

Can you elaborate more as to the desired result?


Here's what I would like to do. For the inner gridview, I would like to show all the images not only on the first row as what I have on the code but rather to all rows. Please see screenshot for sample.

Be able to edit the last column of the outer gridview to change the Info column. (But having error when clicking on Edit. I'll try what you suggested tho.)

Lastly, have a button to hide/show the inner gridview only for the row selected. (I haven't started on this yet due to problems above)

Hope it's clear. If you'd like I could pm you and send you the code I have. Thanks for the help!

Attached image(s)

  • Attached Image

This post has been edited by jpcsmit: 12 December 2009 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Having problems when editing a gridview

Posted 16 December 2009 - 06:43 PM

Sorry to take so long to respond.

Is this still an issue or were you able to resolve it?
Was This Post Helpful? 0
  • +
  • -

#10 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Re: Having problems when editing a gridview

Posted 17 December 2009 - 10:59 AM

I'm not quie sure what I did..now I'm having a "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

Though my query is quite simple:
SQL = "SELECT Entry, Comment, Rating, Whois " _
& "FROM tb_comments " _
& "WHERE Entry='" & lbl3.Text & "'"



I found some articles but those have several SELECT on their queries. Mine only one so I'm not sure why.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1