Solved

Add button problem in Gridview

Posted on 2013-11-29
7
557 Views
Last Modified: 2013-12-02
Hi,
when running the project, using Access DB, I get this

Server Error in '/App17_devst' Application.

Object reference not set to an instance of an object.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:



[NullReferenceException: Object reference not set to an instance of an object.]
   Add_ite._Default.AddNewRec(Object sender, EventArgs e) +784
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3803

 


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408  

once I've clicked the Add button of the Gridview. Here is the event of the Add button

        protected void AddNewRec(object sender, EventArgs e)
        {
            int ite_id = Convert.ToInt16(lb_iteid.Text);
            DropDownList ddl=(DropDownList)GridView1.FooterRow.FindControl("ddl3");
            int int_id=11;
            if (ddl.Items.FindByValue((GridView1.FooterRow.FindControl("tb_int_id") as TextBox).Text) != null)
            {
                int_id = Convert.ToInt16((GridView1.FooterRow.FindControl("tb_int_id") as TextBox).Text);
            }
            string int_desc = ((TextBox)GridView1.FooterRow.FindControl("tb_int_desc")).Text;
            DateTime change_date = Convert.ToDateTime(((Label)GridView1.FooterRow.FindControl("lblchangedate")).Text);
            //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mssqlconn2"].ConnectionString);
            //SqlCommand cmd = new SqlCommand();
            OleDbConnection conn = null;
            conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:Database Password=?????;Data Source=C:\inetpub\VS2012\DB1.accdb");
            OleDbCommand cmd = null, cmd2 = null;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into [ite_tab] ([ite_id], [int_id], [int_desc], [change_date]) " +
            "values(@ite_id, @int_id, @int_desc, @change_date)";
            cmd.Parameters.Add("@ite_id", OleDbType.Integer).Value = ite_id;
            cmd.Parameters.Add("@int_id", OleDbType.Integer).Value = int_id;
            cmd.Parameters.Add("@int_desc", OleDbType.VarChar).Value = int_desc;
            cmd.Parameters.Add("@change_date", OleDbType.DBDate).Value = change_date;
            cmd.ExecuteNonQuery();

            cmd2.CommandType = CommandType.Text;
            cmd2.CommandText = "select [ite_id],[int_id],[int_desc],[change_date] from [ite_tab] where [ite_id]=@ite_id2";
            cmd2.Parameters.Add("@ite_id2", OleDbType.Integer).Value = ite_id;
            GridView1.DataSource = GetData(cmd2);
            GridView1.DataBind();
        }

Open in new window

and here is the Gridview

    <asp:GridView ID="GridView1" runat="server"  Width = "550px"
    AutoGenerateColumns = "false" Font-Names = "Arial"
    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
    HeaderStyle-BackColor = "green" AllowPaging ="true"  
    ShowFooter = "true" OnPageIndexChanging="GridView1_PageIndexChanging" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" 
    OnRowUpdating="GridView1_RowUpdating" PageSize = "10" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
    <asp:TemplateField HeaderText="Item">
        <ItemTemplate>
            <asp:DropDownList ID="ddl" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
            </asp:DropDownList>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddl2" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
            </asp:DropDownList>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:DropDownList ID="ddl3" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
            </asp:DropDownList>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField Visible="false">
        <ItemTemplate>
            <asp:TextBox id="tb_int_id" runat="server" Text='<%# Eval("int_id") %>' Visible="false" />
            <asp:TextBox id="tb_int_desc" runat="server" Text='<%# Eval("int_desc") %>' Visible="false" />
            <asp:TextBox id="tb_int_id2" runat="server" Text='<%# Eval("int_id") %>' Visible="false" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox id="tb_int_id" runat="server" Text='<%# Eval("int_id") %>' Visible="false" />
            <asp:TextBox id="tb_int_desc" runat="server" Text='<%# Eval("int_desc") %>' Visible="false" />
            <asp:TextBox id="tb_int_id2" runat="server" Text='<%# Eval("int_id") %>' Visible="false" />
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox id="tb_int_id" runat="server" Text='<%# Eval("int_id") %>' Visible="false" />
            <asp:TextBox id="tb_int_desc" runat="server" Text='<%# Eval("int_desc") %>' Visible="false" />
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Change Date">
        <ItemTemplate>
            <asp:Label ID="lblchangedate" runat="server"
                Text='<%# Eval("change_date")%>'>
            </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:Label ID="lblchangedate" runat="server" 
                Text='<%# Eval("change_date")%>' >
            </asp:Label>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:Label ID="lblchangedate" runat="server" 
                Text='<%# Eval("change_date")%>' >
            </asp:Label>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Delete?">
        <ItemTemplate>
            <span onclick="return confirm('Are you sure to delete the record?')">
                <asp:LinkButton ID="lnkdelete" runat="server" Text="Delete" ForeColor="Red" CommandName="Delete"/>
            </span>
        </ItemTemplate>
        <FooterTemplate>
            <asp:Button ID="btnadd" runat="server" Text="Add" OnClick="AddNewRec" />
        </FooterTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="true" />
    </Columns>
    <AlternatingRowStyle BackColor="White"/>
    <EditRowStyle BackColor="#efefef" />
    <FooterStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="true" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>

Open in new window

0
Comment
Question by:HuaMinChen
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
What is the line of code @ 784 line number?
0
 
LVL 10

Author Comment

by:HuaMinChen
Comment Utility
Thanks. Here are some codes with line number
770                catch (SqlException ex)
771                {
772                    ...
773                    //return;
774                }
775                catch (Exception ex)
776                {
777                    ...
778                    //return;
779                }
780                finally
781                {
782                    ...
783                    conn.Close();
784                }

Open in new window

0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
Can you try this once

finally
781                {
782                    if(conn != null)
783                    conn.Close();
784                }
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 10

Author Comment

by:HuaMinChen
Comment Utility
Hi,
I've put what you mentioned. Then redeployed it and reran it. But I still get the same problem below once I've clicked "Add" button within Gridview.

Server Error in '/App17_devst' Application.

Object reference not set to an instance of an object.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:



[NullReferenceException: Object reference not set to an instance of an object.]
   Add_user._Default.AddNewRec(Object sender, EventArgs e) +784
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3803

 


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
0
 
LVL 10

Author Comment

by:HuaMinChen
Comment Utility
Any help?
0
 
LVL 15

Accepted Solution

by:
JimFive earned 140 total points
Comment Utility
It looks to me like you might need to set the connection of the command objects:
cmd.connection = conn
cmd2.connection = conn
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
can you send me code written in finally block. It seems after adding code given by me you are still getting error on 784 line number. After applying code given by me should change the line number atleast, if its unresolved.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now