[Solved] Request.Form with Master Page Error: Object reference not set to an instance of an object

Last Reply 11 days ago By pandeyism

Posted 11 days ago

.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="company_sprinkler_data.aspx.cs" Inherits="ait0207_sem7_.WebForm11" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    ....
</asp:Content>

.aspx.cs

namespace ait0207_sem7_
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        protected void sprink_submit_Click(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("INSERT INTO company_sprinkler_data(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)", con);
            string[] textboxValues1 = Request.Form.GetValues("c_email");
            string[] textboxValues2 = Request.Form.GetValues("nozzle_lph");
            string[] textboxValues3 = Request.Form.GetValues("nozzle_price");
            string[] textboxValues4 = Request.Form.GetValues("txt_lat_mm");
            string[] textboxValues5 = Request.Form.GetValues("txt_lat_price");
            string[] textboxValues6 = Request.Form.GetValues("txt_smain_mm");
            string[] textboxValues7 = Request.Form.GetValues("txt_smain_price");
            string[] textboxValues8 = Request.Form.GetValues("txt_main_mm");
            string[] textboxValues9 = Request.Form.GetValues("txt_main_price");
            string[] textboxValues10 = Request.Form.GetValues("txt_mpum_hp");
            string[] textboxValues11 = Request.Form.GetValues("txt_mpump_price");

            int length = 0;
            if (textboxValues2.Length > length)
            {
                length = textboxValues2.Length;
            }
            ......
        }
    }
}

error

Server Error in '/' 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: 


Line 39: 
Line 40:             int length = 0;
Line 41:             if (textboxValues2.Length > length)
Line 42:             {
Line 43:                 length = textboxValues2.Length;

Source File: C:\Users\ait_0207\source\repos\ait0207(sem7)\ait0207(sem7)\company_sprinkler_data.aspx.cs    Line: 41 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   ait0207_sem7_.WebForm11.sprink_submit_Click(Object sender, EventArgs e) in C:\Users\ait_0207\source\repos\ait0207(sem7)\ait0207(sem7)\company_sprinkler_data.aspx.cs:41
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9796134
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +211
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1696

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

so please help me to solve it

 

You are viewing reply posted by: pandeyism 11 days ago.
Posted 11 days ago Modified on 11 days ago

Hi nid@patel,

You can't access control value using Request.Form.GetValues("c_email") in master page, you need write like Request.Form.GetValues(c_email.UniqueID) to access value of control. Refer below sample.

HTML

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <script type="text/javascript">
        function addRow(tableID) {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
            var colCount = table.rows[0].cells.length;
            for (var i = 0; i < colCount; i++) {
                var newcell = row.insertCell(i);
                newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                switch (newcell.childNodes[0].type) {
                    case "text":
                        newcell.childNodes[0].value = "";
                        break;
                    case "checkbox":
                        newcell.childNodes[0].checked = false;
                        break;
                    case "select-one":
                        newcell.childNodes[0].selectedIndex = 0;
                        break;
                }
            }
        }

        function deleteRow(tableID) {
            try {
                var table = document.getElementById(tableID);
                var rowCount = table.rows.length;

                for (var i = 0; i < rowCount; i++) {
                    var row = table.rows[i];
                    var chkbox = row.cells[0].childNodes[1];
                    if (chkbox != null && chkbox.checked == true) {
                        if (rowCount <= 1) {
                            alert("Cannot delete all the rows.");
                            break;
                        }
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }


                }
            } catch (e) {
                alert(e);
            }
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div class="container">
        <h2>
            Sprinkler Irrigation Component's price <small></small>
        </h2>
        <ul class="responsive-table">
            <li class="table-row">
                <div class="col col-1" data-label="Job Id">
                    Email:</div>
                <div class="col col-2" data-label="Customer Name">
                    <asp:TextBox CssClass="form-control" ID="c_email" placeholder="Email" runat="server"></asp:TextBox>
                </div>
                <div class="col col-3" data-label="Amount">
                </div>
            </li>
        </ul>
    </div>
    <div class="container">
        <ul class="responsive-table">
            <li class="table-row">
                <div class="col col-1" data-label="Job Id">
                    Nozzle</div>
                <div class="col col-2">
                    <table id="dataTable1" class="table-bordered table-hover table-striped table">
                        <tr>
                            <td>
                                <input type="checkbox" name="chk" />
                            </td>
                            <td>
                                <asp:TextBox ID="nozzle_lph" runat="server" type="text" placeholder="LPH"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="nozzle_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="col col-3">
                </div>
                <div class="col col-4">
                    <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable1')" /><hr />
                    <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable1')" />
                </div>
            </li>
        </ul>
    </div>
    <div class="container">
        <ul class="responsive-table">
            <li class="table-row">
                <div class="col col-1" data-label="Job Id">
                    Lateral</div>
                <div class="col col-2">
                    <table id="dataTable2" class="table-bordered table-hover table-striped table">
                        <tr>
                            <td>
                                <input type="checkbox" name="chk" />
                            </td>
                            <td>
                                <asp:TextBox ID="txt_lat_mm" runat="server" type="text" placeholder="LPH"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_lat_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="col col-3">
                </div>
                <div class="col col-4">
                    <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable2')" /><hr />
                    <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable2')" />
                </div>
            </li>
        </ul>
    </div>
    <div class="container">
        <ul class="responsive-table">
            <li class="table-row">
                <div class="col col-1" data-label="Job Id">
                    Submain Pipe</div>
                <div class="col col-2">
                    <table id="dataTable3" class="table-bordered table-hover table-striped table">
                        <tr>
                            <td>
                                <input type="checkbox" name="chk" />
                            </td>
                            <td>
                                <asp:TextBox ID="txt_smain_mm" runat="server" type="text" placeholder="LPH"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_smain_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="col col-3">
                </div>
                <div class="col col-4">
                    <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable3')" /><hr />
                    <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable3')" />
                </div>
            </li>
        </ul>
    </div>
    <div class="container">
        <ul class="responsive-table">
            <li class="table-row">
                <div class="col col-1" data-label="Job Id">
                    Main Pipe</div>
                <div class="col col-2">
                    <table id="dataTable4" class="table-bordered table-hover table-striped table">
                        <tr>
                            <td>
                                <input type="checkbox" name="chk" />
                            </td>
                            <td>
                                <asp:TextBox ID="txt_main_mm" runat="server" type="text" placeholder="LPH"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_main_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="col col-3">
                </div>
                <div class="col col-4">
                    <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable4')" /><hr />
                    <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable4')" />
                </div>
            </li>
        </ul>
    </div>
    <div class="container">
        <ul class="responsive-table">
            <li class="table-row">
                <div class="col col-1" data-label="Job Id">
                    Motor Pumping Unit</div>
                <div class="col col-2">
                    <table id="dataTable5" class="table-bordered table-hover table-striped table">
                        <tr>
                            <td>
                                <input type="checkbox" name="chk" />
                            </td>
                            <td>
                                <asp:TextBox ID="txt_mpum_hp" runat="server" type="text" placeholder="LPH"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_mpump_price" runat="server" type="text" placeholder="RS./unit"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="col col-3">
                </div>
                <div class="col col-4">
                    <input type="button" class="btn btn-danger active" value="Add Row" onclick="addRow('dataTable5')" /><hr />
                    <input type="button" class="btn btn-info active" value="Delete Row" onclick="deleteRow('dataTable5')" />
                </div>
            </li>
            <li class="table-row">
                <div class="col col-1" data-label="Job Id">
                </div>
                <div class="col col-2" data-label="Customer Name">
                    <asp:Button ID="sprink_submit" runat="server" CssClass="btn btn-danger btn-group-lg active"
                        Text="Submit" OnClick="sprink_submit_Click" />
                </div>
                <div class="col col-3" data-label="Amount">
                    <asp:Button ID="Button2" runat="server" CssClass="btn btn-warning btn-group-lg active"
                        Text="Back" />
                </div>
            </li>
        </ul>
    </div>
</asp:Content>

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

protected void sprink_submit_Click(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("INSERT INTO company_sprinkler_data(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)");
    string[] textboxValues1 = Request.Form.GetValues(c_email.UniqueID);
    string[] textboxValues2 = Request.Form.GetValues(nozzle_lph.UniqueID);
    string[] textboxValues3 = Request.Form.GetValues(nozzle_price.UniqueID);
    string[] textboxValues4 = Request.Form.GetValues(txt_lat_mm.UniqueID);
    string[] textboxValues5 = Request.Form.GetValues(txt_lat_price.UniqueID);
    string[] textboxValues6 = Request.Form.GetValues(txt_smain_mm.UniqueID);
    string[] textboxValues7 = Request.Form.GetValues(txt_smain_price.UniqueID);
    string[] textboxValues8 = Request.Form.GetValues(txt_main_mm.UniqueID);
    string[] textboxValues9 = Request.Form.GetValues(txt_main_price.UniqueID);
    string[] textboxValues10 = Request.Form.GetValues(txt_mpum_hp.UniqueID);
    string[] textboxValues11 = Request.Form.GetValues(txt_mpump_price.UniqueID);
    int length = 0;
    if (textboxValues2.Length > length)
    {
        length = textboxValues2.Length;
    }
    else if (textboxValues3.Length > length)
    {
        length = textboxValues3.Length;
    }
    else if (textboxValues4.Length > length)
    {
        length = textboxValues4.Length;
    }
    else if (textboxValues5.Length > length)
    {
        length = textboxValues5.Length;
    }
    else if (textboxValues6.Length > length)
    {
        length = textboxValues6.Length;
    }
    else if (textboxValues7.Length > length)
    {
        length = textboxValues7.Length;
    }
    else if (textboxValues8.Length > length)
    {
        length = textboxValues8.Length;
    }
    else if (textboxValues9.Length > length)
    {
        length = textboxValues9.Length;
    }
    else if (textboxValues10.Length > length)
    {
        length = textboxValues10.Length;
    }
    else if (textboxValues11.Length > length)
    {
        length = textboxValues11.Length;
    }

    for (int i = 0; i < length; i++)
    {
        cmd = new SqlCommand("INSERT INTO company_sprinkler_data(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)");
        cmd.Parameters.AddWithValue("@company_email", textboxValues1[0]);
        cmd.Parameters.AddWithValue("@nozzle_lph", textboxValues2.Length > i ? textboxValues2[i] : "");
        cmd.Parameters.AddWithValue("@nozzle_price", textboxValues3.Length > i ? textboxValues3[i] : "");
        cmd.Parameters.AddWithValue("@lateral_mm", textboxValues4.Length > i ? textboxValues4[i] : "");
        cmd.Parameters.AddWithValue("@lateral_price", textboxValues5.Length > i ? textboxValues5[i] : "");
        cmd.Parameters.AddWithValue("@main_mm", textboxValues6.Length > i ? textboxValues6[i] : "");
        cmd.Parameters.AddWithValue("@main_price", textboxValues7.Length > i ? textboxValues7[i] : "");
        cmd.Parameters.AddWithValue("@submain_mm", textboxValues8.Length > i ? textboxValues8[i] : "");
        cmd.Parameters.AddWithValue("@submain_price", textboxValues9.Length > i ? textboxValues9[i] : "");
        cmd.Parameters.AddWithValue("@Sp_mpump_hp", textboxValues10.Length > i ? textboxValues10[i] : "");
        cmd.Parameters.AddWithValue("@Sp_mpump_price", textboxValues11.Length > i ? textboxValues11[i] : "");
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        c_email.Text = " ";
    }
}

VB.Net

Protected Sub sprink_submit_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim constr As String = ConfigurationManager.ConnectionStrings("Mycon").ConnectionString
    Dim con As SqlConnection = New SqlConnection(constr)
    Dim cmd As SqlCommand = New SqlCommand("INSERT INTO company_sprinkler_data(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)")
    Dim textboxValues1 As String() = Request.Form.GetValues(c_email.UniqueID)
    Dim textboxValues2 As String() = Request.Form.GetValues(nozzle_lph.UniqueID)
    Dim textboxValues3 As String() = Request.Form.GetValues(nozzle_price.UniqueID)
    Dim textboxValues4 As String() = Request.Form.GetValues(txt_lat_mm.UniqueID)
    Dim textboxValues5 As String() = Request.Form.GetValues(txt_lat_price.UniqueID)
    Dim textboxValues6 As String() = Request.Form.GetValues(txt_smain_mm.UniqueID)
    Dim textboxValues7 As String() = Request.Form.GetValues(txt_smain_price.UniqueID)
    Dim textboxValues8 As String() = Request.Form.GetValues(txt_main_mm.UniqueID)
    Dim textboxValues9 As String() = Request.Form.GetValues(txt_main_price.UniqueID)
    Dim textboxValues10 As String() = Request.Form.GetValues(txt_mpum_hp.UniqueID)
    Dim textboxValues11 As String() = Request.Form.GetValues(txt_mpump_price.UniqueID)
    Dim length As Integer = 0

    If textboxValues2.Length > length Then
        length = textboxValues2.Length
    ElseIf textboxValues3.Length > length Then
        length = textboxValues3.Length
    ElseIf textboxValues4.Length > length Then
        length = textboxValues4.Length
    ElseIf textboxValues5.Length > length Then
        length = textboxValues5.Length
    ElseIf textboxValues6.Length > length Then
        length = textboxValues6.Length
    ElseIf textboxValues7.Length > length Then
        length = textboxValues7.Length
    ElseIf textboxValues8.Length > length Then
        length = textboxValues8.Length
    ElseIf textboxValues9.Length > length Then
        length = textboxValues9.Length
    ElseIf textboxValues10.Length > length Then
        length = textboxValues10.Length
    ElseIf textboxValues11.Length > length Then
        length = textboxValues11.Length
    End If

    For i As Integer = 0 To length - 1
        cmd = New SqlCommand("INSERT INTO company_sprinkler_data(company_email,nozzle_lph,nozzle_price,lateral_mm,lateral_price,main_mm,main_price,submain_mm,submain_price,Sp_mpump_hp,Sp_mpump_price)values(@company_email,@nozzle_lph,@nozzle_price,@lateral_mm,@lateral_price,@main_mm,@main_price,@submain_mm,@submain_price,@Sp_mpump_hp,@Sp_mpump_price)")
        cmd.Parameters.AddWithValue("@company_email", textboxValues1(0))
        cmd.Parameters.AddWithValue("@nozzle_lph", If(textboxValues2.Length > i, textboxValues2(i), ""))
        cmd.Parameters.AddWithValue("@nozzle_price", If(textboxValues3.Length > i, textboxValues3(i), ""))
        cmd.Parameters.AddWithValue("@lateral_mm", If(textboxValues4.Length > i, textboxValues4(i), ""))
        cmd.Parameters.AddWithValue("@lateral_price", If(textboxValues5.Length > i, textboxValues5(i), ""))
        cmd.Parameters.AddWithValue("@main_mm", If(textboxValues6.Length > i, textboxValues6(i), ""))
        cmd.Parameters.AddWithValue("@main_price", If(textboxValues7.Length > i, textboxValues7(i), ""))
        cmd.Parameters.AddWithValue("@submain_mm", If(textboxValues8.Length > i, textboxValues8(i), ""))
        cmd.Parameters.AddWithValue("@submain_price", If(textboxValues9.Length > i, textboxValues9(i), ""))
        cmd.Parameters.AddWithValue("@Sp_mpump_hp", If(textboxValues10.Length > i, textboxValues10(i), ""))
        cmd.Parameters.AddWithValue("@Sp_mpump_price", If(textboxValues11.Length > i, textboxValues11(i), ""))
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
        c_email.Text = " "
    Next
End Sub