473,387 Members | 1,470 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,387 software developers and data experts.

GridView Error on Select: Object reference not set to an instance of an object

JustRun
127 100+
I have a problem with the select option at the gridview and gonna pull my hair out cause it doesn't work at all.
Here is the Grid view code: I want to get the selected row when the user click "Select"

Expand|Select|Wrap|Line Numbers
  1.   <asp:GridView ID="grdDept" runat="server" AllowPaging="True" AllowSorting="True"
  2.             AutoGenerateColumns="False" DataKeyNames="DocumentID" DataSourceID="sdsDepartment"
  3.             Width="600px" CellPadding="4" ForeColor="#333333" GridLines="None">
  4.             <Columns>
  5.                 <asp:TemplateField ShowHeader="False">
  6.                     <EditItemTemplate>
  7.                         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
  8.                             Text="Update"></asp:LinkButton>
  9.                         <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
  10.                             Text="Cancel"></asp:LinkButton>
  11.                     </EditItemTemplate>
  12.                     <ItemTemplate>
  13.                         <asp:LinkButton ID="btnEdit" runat="server" CausesValidation="False" CommandName="Edit"
  14.                             Text="Edit"></asp:LinkButton>
  15.                         <asp:LinkButton ID="btnSlct" runat="server" CausesValidation="False"
  16.                             Text="Select" CommandName="Select"></asp:LinkButton>
  17.                         <asp:LinkButton ID="btnDelete" runat="server" CausesValidation="False" CommandName="Delete"
  18.                             Text="Delete"></asp:LinkButton>
  19.                     </ItemTemplate>
  20.                 </asp:TemplateField>
  21.                 <asp:TemplateField HeaderText="DocumentID" SortExpression="DocumentID">
  22.                     <EditItemTemplate>
  23.                         <asp:Label ID="Label1" runat="server" Text='<%# Eval("DocumentID") %>'></asp:Label>
  24.                     </EditItemTemplate>
  25.                     <ItemTemplate>
  26.                         <asp:Label ID="Label1" runat="server" Text='<%# Bind("DocumentID") %>'></asp:Label>
  27.                     </ItemTemplate>
  28.                 </asp:TemplateField>
  29.                 <asp:TemplateField HeaderText="Department" SortExpression="DeptName">
  30.                     <EditItemTemplate>
  31.                         <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DeptName") %>'></asp:TextBox>
  32.                     </EditItemTemplate>
  33.                     <ItemTemplate>
  34.                         <asp:Label ID="lblDeptName" runat="server" Text='<%# Bind("DeptName") %>' Width="300px"></asp:Label>
  35.                     </ItemTemplate>
  36.                     <HeaderStyle Width="150px" />
  37.                 </asp:TemplateField>
  38.             </Columns>
  39.             <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  40.             <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  41.             <EditRowStyle BackColor="#999999" />
  42.             <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  43.             <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  44.             <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  45.             <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  46.         </asp:GridView>
  47.  
In The Code Behind I tried many thing and all of them render the same error. "Object reference not set to an instance of an object".


Expand|Select|Wrap|Line Numbers
  1.  
  2.     protected void grdDept_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  3.     {
  4.         Label lblDeptName = (Label)grdDept.FindControl("lblDeptName");
  5.         lblErr.Text = lblDeptName.Text;
  6.     }  Also i tried this:       protected void grdDept_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  7.     {
  8.         string strDept = grdDept.Rows[e.NewSelectedIndex].Cells[2].Text;
  9.         lblErr.Text = strDept;
  10.     }
  11.  
Sep 30 '08 #1
15 11629
nateraaaa
663 Expert 512MB
Could you post the server error you are receiving? The error should tell you what object is null.

Nathan
Sep 30 '08 #2
JustRun
127 100+
This is the error :

Object reference not set to an instance of an object

Also i tried to catch the error but the catch got Nothing :_(
Sep 30 '08 #3
Plater
7,872 Expert 4TB
Did you set breakpoints and step through your code?
Sep 30 '08 #4
JustRun
127 100+
No actually

I dont know How to do it
Sep 30 '08 #5
Plater
7,872 Expert 4TB
Place break point at the begining of your code behind function.
Hit F5 (to run in debug mode) then navigate the browser to the correct page that casues the error.
Sep 30 '08 #6
JustRun
127 100+
There is no error but guess what:

I tried this to just test if the evnt work or not And I hot NOTHING displayed in the text box :

Expand|Select|Wrap|Line Numbers
  1.     protected void grdDept_SelectedIndexChanged(object sender, EventArgs e)
  2.     {
  3.         txtTest.Text = "Null";
  4.     }
  5.  
Sep 30 '08 #7
Plater
7,872 Expert 4TB
Somehow you have two controls with the same ID (when looking at your original post)
I cannot believe the program actually compiles without any errors/warnings about that.
Sep 30 '08 #8
JustRun
127 100+
I've checked it well, and didn't find any conflict at the controls names.

Please tell me where is the conflict u r talking about?

Thanks
Sep 30 '08 #9
Plater
7,872 Expert 4TB
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("DocumentID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DocumentID") %>'></asp:Label>
</ItemTemplate>
Sep 30 '08 #10
JustRun
127 100+
Yes they have the same name but there is no conflict cause each of them inside a template., They are not in the same template.
Sep 30 '08 #11
Frinavale
9,735 Expert Mod 8TB
Yes they have the same name but there is no conflict cause each of them inside a template., They are not in the same template.
Are you using DataBind on the GridView in your PageLoad event?
If you do this all you will reset the data source for the GridView and delete any values that would have been entered by the end user during editing.

-Frinny
Sep 30 '08 #12
JustRun
127 100+
Sorry Couldn't understand you!

Please Clarify more, But at any rate I bind my data through SqlDataSource

Thanks
Sep 30 '08 #13
Frinavale
9,735 Expert Mod 8TB
Sorry Couldn't understand you!

Please Clarify more, But at any rate I bind my data through SqlDataSource

Thanks
Please post the code in your page load event.
Oct 1 '08 #14
JustRun
127 100+
hI,
Sorry for the late reply, Here is the Code,
Note : The Commented Part is the part that not work and i want to fix it. but the uncommented part is the default Gridview this works fine.

Expand|Select|Wrap|Line Numbers
  1.             <asp:SqlDataSource ID="sdsAllDepts" runat="server" ConflictDetection="CompareAllValues"
  2.                 ConnectionString="Data Source=.;Initial Catalog=TimeSheet;Persist Security Info=True;User ID=sa;Password=sa"
  3.                 DeleteCommand="DELETE FROM [DepartmentTable] WHERE [DocumentID] = @original_DocumentID AND [DeptName] = @original_DeptName"
  4.                 InsertCommand="INSERT INTO [DepartmentTable] ([DeptName], [DocumentID]) VALUES (@DeptName, @DocumentID)"
  5.                 OldValuesParameterFormatString="original_{0}" ProviderName="System.Data.SqlClient"
  6.                 SelectCommand="SELECT [DeptName], [DocumentID] FROM [DepartmentTable] ORDER BY [DeptName]"
  7.                 UpdateCommand="UPDATE [DepartmentTable] SET [DeptName] = @DeptName WHERE [DocumentID] = @original_DocumentID AND [DeptName] = @original_DeptName" OnUpdated="sdsAllDepts_Updated">
  8.                 <DeleteParameters>
  9.                     <asp:Parameter Name="original_DocumentID" Type="Object" />
  10.                     <asp:Parameter Name="original_DeptName" Type="String" />
  11.                 </DeleteParameters>
  12.                 <UpdateParameters>
  13.                     <asp:Parameter Name="DeptName" Type="String" />
  14.                     <asp:Parameter Name="original_DocumentID" Type="Object" />
  15.                     <asp:Parameter Name="original_DeptName" Type="String" />
  16.                 </UpdateParameters>
  17.                 <InsertParameters>
  18.                     <asp:Parameter Name="DeptName" Type="String" />
  19.                     <asp:Parameter Name="DocumentID" Type="Object" />
  20.                 </InsertParameters>
  21.             </asp:SqlDataSource>
  22.             <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
  23. <%--            <asp:GridView ID="grdDept" runat="server" AllowPaging="True" AllowSorting="True"
  24.                 AutoGenerateColumns="False" DataKeyNames="DocumentID"
  25.                 DataSourceID="sdsAllDepts" Width="685px" OnSelectedIndexChanging="grdDept_SelectedIndexChanging" CellPadding="4" ForeColor="#333333" GridLines="None">
  26.                 <Columns>
  27.                     <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True">
  28.                         <HeaderStyle Width="150px" />
  29.                     </asp:CommandField>
  30.                     <asp:TemplateField HeaderText="Department Name" SortExpression="DeptName">
  31.                         <EditItemTemplate>
  32.                             <table style="width: 535px">
  33.                                 <tr>
  34.                                     <td style="width: 310px">
  35.                                         &nbsp;<asp:TextBox ID="txtDeptName" runat="server" Text='<%# Bind("DeptName") %>' Width="300px" MaxLength="256"></asp:TextBox></td>
  36.                                     <td style="width: 20px">
  37.                                         <asp:RequiredFieldValidator
  38.                                 ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtDeptName" ErrorMessage="Please enter a department name"
  39.                                 Text="*"></asp:RequiredFieldValidator><asp:RegularExpressionValidator ID="RegularExpressionValidator1"
  40.                                     runat="server" ControlToValidate="txtDeptName" ErrorMessage="Department name should not allow Special Characters And Not Less Than 3 digits"
  41.                                     ValidationExpression="\w+([\s]\w+)*">*</asp:RegularExpressionValidator></td>
  42.                                     <td style="width: 235px">
  43.                                         <asp:Label ID="Label12" runat="server" CssClass="formfont" ForeColor="White" Text="at least 3 digits with spaces"></asp:Label></td>
  44.                                 </tr>
  45.                             </table>
  46.                         </EditItemTemplate>
  47.                         <ItemTemplate>
  48.                             <asp:Label ID="Label1" runat="server" Text='<%# Bind("DeptName") %>' Width="300px"></asp:Label>
  49.                         </ItemTemplate>
  50.                     </asp:TemplateField>
  51.                     <asp:BoundField DataField="DocumentID" HeaderText="DocumentID" ReadOnly="True" SortExpression="DocumentID"
  52.                         Visible="False" />
  53.                 </Columns>
  54.                 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  55.                 <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  56.                 <EditRowStyle BackColor="#999999" />
  57.                 <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  58.                 <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  59.                 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  60.                 <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  61.                 <EmptyDataTemplate>
  62.                     <span style="color: darkslateblue">There is no data curruntly,<br />
  63.                         If you want to add new departmnet, please follow</span>
  64.                     <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/administrator/NewDepartment.aspx">this link</asp:HyperLink>
  65.                 </EmptyDataTemplate>
  66.             </asp:GridView>--%>
  67.             <asp:GridView ID="grdDept" runat="server" AllowPaging="True" AllowSorting="True"
  68.                 AutoGenerateColumns="False" DataKeyNames="DocumentID"
  69.                 DataSourceID="sdsAllDepts" Width="600px" OnSelectedIndexChanging="grdDept_SelectedIndexChanging" CellPadding="4" ForeColor="#333333" GridLines="None">
  70.                 <Columns>
  71.                     <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
  72.                     <asp:BoundField DataField="DeptName" HeaderText="Department Name" SortExpression="DeptName" />
  73.                     <asp:BoundField DataField="DocumentID" HeaderText="DocumentID" ReadOnly="True" SortExpression="DocumentID"
  74.                         Visible="False" />
  75.                 </Columns>
  76.                 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  77.                 <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  78.                 <EditRowStyle BackColor="#999999" />
  79.                 <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  80.                 <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  81.                 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  82.                 <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  83.                 <EmptyDataTemplate>
  84.                     <span style="color: darkslateblue">There is no data curruntly,<br />
  85.                         If you want to add new departmnet, please follow</span>
  86.                     <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/administrator/NewDepartment.aspx">this link</asp:HyperLink>
  87.                 </EmptyDataTemplate>
  88.             </asp:GridView>
  89.             <br />
  90.             <asp:Label ID="lblErr" runat="server" ForeColor="Red" Visible="False"></asp:Label><asp:HiddenField
  91.                 ID="txtDept" runat="server" />
  92.             <br />
  93.             <br />
  94.             <asp:Label ID="lblDept" runat="server" Font-Bold="True"></asp:Label>
  95.             <br />
  96.             <br />
  97.             <hr />
  98.  
Here is the C# Code, Also this is the workng part And I've tried many thing to get the data from Template Gridview like: (rowcommand, rowdatabound, btnSlct_Click)

Expand|Select|Wrap|Line Numbers
  1.     protected void grdDept_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  2.     {
  3.         lblErr.Visible = false;
  4.         grdDetails.Visible = true;
  5.         string dept = grdDept.Rows[e.NewSelectedIndex].Cells[1].Text;
  6.         lblDept.Text = "Employees at '" + dept + "' department";
  7.         txtDept.Value = dept;
  8.     }
  9.  
Oct 1 '08 #15
Any Idea which line is Causing it to say that..


Once you get the line number, you can then see which objects on that line are being dereferenced. The exception is thrown because one of the objects being dereferenced is not yet initialized.
Oct 6 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bandit | last post by:
I'm populating a gridview (called docList) with document info from a network folder like so: Sub Show_Files(ByVal inputDir As String) Dim strFileNamePath As String = inputDir Dim dirInfo As...
1
by: John_H | last post by:
Re: ASP.NET 2.0 I would like suggestions or code examples on how to collect a variable length list of input data (item# & item quantity specifically). I thought that I could accomplish this...
3
by: Badis | last post by:
Hi Guys, I have a link button in TemplateField in a gridview called "transactionListGridView" and when tying to run this: protected void transactionListGridView_RowCommand(object sender,...
3
by: needin4mation | last post by:
In this code: protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataRowView rowView =...
1
by: jobs | last post by:
Given the Gridview below: When I select the button "Rate" and fall into this code. How can I reference "carriercode" and others . Apparently selecting the button does not select the row. I've...
2
by: Greg | last post by:
Hello, I am trying to bind a GridView to a custom object I have created. First, here is what I'm trying to do: I have a wizard for adding/editing Users. When the wizard begins, a User...
0
by: trint | last post by:
I hope I can explain this clearly enough to get help. I have three gridviews that need to work from each other to display like a java verticle menu (just in the way it appears). GridView1 is...
1
by: David C | last post by:
I have a GridView of contacts that contains an embedded gridview of multiple phones for that particular contact. I have a LinkButton on the contacts GridView that refers to the Select command and...
14
by: Mike | last post by:
I have a gridview that has a button in the last cell of each row. I want to get the text from cell 1 for that row the button was clicked on. So if my gridview looks like this: N-12 BMW ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.