locked
how to send gridview page data to source page textboxes? RRS feed

  • Question

  • User-1026236167 posted

    i have two webpages one is gridview table format data Gridview table format when click on button gridview data does not fill to sourcepage textboxes source page an error occur after clicking button (object reference not set to an instance)

    gridview buttonclick code

    protected void BT_submit1_Click(object sender, EventArgs e) {

            Response.Redirect("generate salary.aspx?Benefited_leaves1 =" +  
            Gv2.SelectedRow.FindControl("bn2"));
        }

    source page pageload code

    protected void Page_Load(object sender, EventArgs e) {

            foreach (GridViewRow gr1 in Gv2.Rows)
            {
                string benefit = "0";
                TextBox tb = (TextBox)gr1.FindControl("bn2");
                benefit = ((tb.Text));
    
                TextBox bn15 = ((TextBox)gr1.FindControl("bn2"));
                bn15.Text = benefit;
    
                bn15.Text = Request.QueryString["Benefited_leaves1"].ToString();
            }

    }

    Wednesday, April 8, 2020 4:02 AM

Answers

  • User-719153870 posted

    Hi prabhjot1313,

    Since you provided the complete code, now we can find the reason for this "object reference not set to an instance" issue is that you passed nothing to this parameter Benefited_leaves1 in yout URL Response.Redirect("webform49.aspx?Benefited_leaves1 =" + bn2.Text); while the tostring() method in your source page's pageload event requires an object.

    You may wonder why i'm saying you passed nothing to this parameter when you literally add the bn2.Text to the URL, this i was wondering too but it finally turns out that there's a sapce between the parameter name and value just before the "=" in your URL: "webform49.aspx?Benefited_leaves1{here's the little space}=" + bn2.Text.

    Another thing is, i would suggest you to store your value in an asp.net control such as a label control so that you can easily get its value from server code. For example:

                                            <tr style="border: 1px solid black;">
                                                <td>Benefited Leaves</td>
                                                <td><%# Eval("Benefited_leaves") %></td>
                                                <td>
                                                    <asp:Label ID="bn2" runat="server" Text='<%# Eval("Benefited_leaves1") %>'></asp:Label>
                                                </td>
                                            </tr>

    And change your server code like below:

            protected void BT_submit11_Click(object sender, EventArgs e)
            {
                //GridViewRow gr = Gv2.SelectedRow;
                //Response.Redirect("webform49.aspx?Basic_allowance1 =" + gr.Cells[0].FindControl("Basic_allowance1").ToString());
    
                Button btn1 = (Button)sender;
                GridViewRow row = (GridViewRow)btn1.Parent.Parent;
                Label bn2 = (Label)row.FindControl("bn2");
                Response.Redirect("webform49.aspx?Benefited_leaves1=" + bn2.Text);
            }

    PS, if this helps solve your issue, please mark it as the answer. Thanks!

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2020 4:59 AM

All replies

  • User-719153870 posted

    Hi prabhjot1313,

    Gv2.SelectedRow returns null in a button click event, the GridView.SelectedRow Property works in the SelectedIndexChanging event.

    To get the control in the same row by clicking a button in GridView, you need to find the GridViewRow of this button and then find the control.

    Check below demo:

    aspx:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:TextBox ID="bn2" runat="server" Text='<%#Eval("FirstName") %>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    

    cs:

            protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[1] { new DataColumn("FirstName") });
                dt.Rows.Add("AA");
                dt.Rows.Add("BB");
                dt.Rows.Add("CC");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Button btn1 = (Button)sender;
                GridViewRow row = (GridViewRow)btn1.Parent.Parent;
                TextBox bn2 = (TextBox)row.FindControl("bn2");
                Response.Redirect("generate salary.aspx?Benefited_leaves1 =" + bn2.Text);
            }

    Best Regard,

    Yang Shen

    Thursday, April 9, 2020 1:57 AM
  • User-1026236167 posted

    see this

    1st page where gridview data send to 2nd page  on buttonclick error occur object reference not set to an instance

    1st page

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;

    namespace WebApplication14
    {
    public partial class WebForm48 : System.Web.UI.Page
    {
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();


    public void EstablishConnection(string storeprocedure)
    {
    con.ConnectionString = connection;
    cmd.Connection = con;
    cmd.Connection.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = storeprocedure;
    }


    public void CloseConnection()
    {
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    con.Close();
    }

    public void FillGridview()
    {
    SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_gen_salary where tbl_gen_salary.month = '" + month.SelectedValue.ToString() + "' AND tbl_gen_salary.year = '" + year.SelectedValue.ToString() + "'", connection);
    DataTable DT = new DataTable();
    adp.Fill(DT);
    Gv2.DataSource = DT;
    Gv2.DataBind();

    }

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void BT_submit_Click(object sender, EventArgs e)
    {
    FillGridview();
    }

    protected void BT_submit11_Click(object sender, EventArgs e)
    {
    // GridViewRow gr = Gv2.SelectedRow;
    //Response.Redirect("webform49.aspx?Basic_allowance1 =" + gr.Cells[0].FindControl("Basic_allowance1").ToString());

    Button btn1 = (Button)sender;
    GridViewRow row = (GridViewRow)btn1.Parent.Parent;
    TextBox bn2 = (TextBox)row.FindControl("bn2");
    Response.Redirect("webform49.aspx?Benefited_leaves1 =" + bn2.Text);


    }

    }
    }

    here is aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="WebForm48.aspx.cs" Inherits="WebApplication14.WebForm48" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server">



    <table>
    <tr>
    <td>
    <span style="margin-left:40px; color:black">Select Month</span><br />

    <span style="margin-left:40px; border-radius:5px;">
    <asp:DropDownList runat="server" ID="month" Width="220px" Height="35px">
    <asp:ListItem Value="0">Select Month</asp:ListItem>
    <asp:ListItem>January</asp:ListItem>
    <asp:ListItem>February</asp:ListItem>
    <asp:ListItem>March</asp:ListItem>
    <asp:ListItem>April</asp:ListItem>
    <asp:ListItem>May</asp:ListItem>
    <asp:ListItem>June</asp:ListItem>
    <asp:ListItem>July</asp:ListItem>
    <asp:ListItem>August</asp:ListItem>
    <asp:ListItem>September</asp:ListItem>
    <asp:ListItem>October</asp:ListItem>
    <asp:ListItem>November</asp:ListItem>
    <asp:ListItem>December</asp:ListItem>

    </asp:DropDownList> </span><br />

    </td>

    <td>
    <span style="margin-left:40px; color:black">Select Year</span><br />

    <span style="margin-left:40px; border-radius:5px;">
    <asp:DropDownList runat="server" ID="year" Width="220px" Height="35px">
    <asp:ListItem Value="0">Select Year</asp:ListItem>
    <asp:ListItem>2019</asp:ListItem>
    <asp:ListItem>2020</asp:ListItem>

    </asp:DropDownList> </span><br />

    </td>
    </tr>
    </table>

    <span style="margin-left:50px"><asp:Button ID="BT_submit" runat="server" Text="Generate Salary"
    OnClick="BT_submit_Click" Height="35px" Width="135px" BackColor="#3366CC" ForeColor="White"
    Style="border-radius:8px" /></span>

    <div style="margin-left:40px;">


    <asp:GridView runat="server" ID="Gv2" CellPadding="4" ForeColor="#333333" GridLines="None"
    AutoGenerateColumns="false" Width="50%"
    OnSelectedIndexChanged="BT_submit11_Click" >
    <AlternatingRowStyle BackColor="White" />

    <Columns>

    <asp:TemplateField >
    <ItemTemplate>
    <div style = "border-collapse: collapse; border: 1px solid black; width: 100%; height: 305px; ">
    <table style = " font-size:13px; border-collapse: collapse; ">
    <tr style = "border: 1px solid black; width:70%" >
    <th style="margin-left:-10px"> Sr.No </th> <th style="width:70px"> Month </th> <th style="width:50px">Year </th> <th style="width:70px"> Employee<br /> name </th><th style="width:70px"> Basic<br /> Salary</th><th style="width:270px">Addition </th>
    <th style="width:250px">Deduction</th><th>Final Salary</th>

    </tr>
    <tr style = "border: 1px solid black;" >
    <td><%# Eval("tbl_id") %></td>
    <td><%# Eval("month") %></td>
    <td><%# Eval("year") %></td>
    <td><%# Eval("employee_name") %></td>
    <td><%# Eval("employee_salary") %></td>
    </tr>

    <div >
    <table style="margin-left:295px; border-collapse: collapse; margin-top:-22px; font-size:12px; padding: 10px 2% 10px 10px; border: 1px solid black; width:250px; " >
    <tr style = "border: 1px solid black;" >
    <th >Particular</th>
    <th>Figure</th>
    <th>Amount</th>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Any Credit</td><td>1</td><td><%# Eval("any_credit") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Basic Allowance</td><td><%# Eval("Basic_allowance") %></td><td><%# Eval("Basic_allowance1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Benefited Leaves</td><td ><%# Eval("Benefited_leaves") %></td><td id="bn2"><%# Eval("Benefited_leaves1") %></td>

    </tr>
    <tr style = "border: 1px solid black;">

    <td>College</td><td><%# Eval("college") %></td><td><%# Eval("college1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Embassy</td><td><%# Eval("Embassy") %></td><td><%# Eval("Embassy1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Enrollment</td><td><%# Eval("Enrollment") %></td><td><%# Eval("Enrollment1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Fixed Travel Expenses</td><td><%# Eval("Fixed_travel_expenses") %></td><td><%# Eval("Fixed_travel_expenses1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Petrol</td><td><%# Eval("Petrol") %></td><td><%# Eval("Petrol1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Visa Grant</td><td><%# Eval("visa_grant") %></td><td><%# Eval("visa_grant1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Visa Refusal</td><td><%# Eval("visa_refusal") %></td><td><%# Eval("visa_refusal1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Total</td><td> </td><td><%# Eval("total_addition") %></td>
    </tr>
    </table>
    <table style="margin-left:545px; margin-top:-255px; font-size:13px; padding: 10px 2% 10px 10px; border:1px solid black; width:70px">
    <tr style = "border: 1px solid black;">
    <th>Particular</th>
    <th>Figure</th>
    <th>Amount</th>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Adcent</td><td><%# Eval("absent") %></td><td><%# Eval("absent1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Advance</td><td> </td><td><%# Eval("advance") %></td>
    </tr>
    <tr>
    <td>Any Deduction</td><td>1</td><td><%# Eval("any_deduction") %></td>

    </tr>
    <tr style = "border: 1px solid black;">
    <td>Half Day</td><td><%# Eval("Halfdays") %></td><td><%# Eval("Halfdays1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Short</td><td><%# Eval("short") %></td><td><%# Eval("short1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Sunday</td><td><%# Eval("sunday") %></td><td><%# Eval("sunday1") %></td>
    </tr>
    <tr style = "border: 1px solid black;">
    <td>Total</td><td></td><td><%# Eval("total_Subtraction") %></td>
    </tr>
    </table>

    </div>

    <div style = "border: 1px solid black;" >
    <table style="margin-left:730px; margin-top:-265px; border: 1px solid black; width:70px; font-size:13px; " >
    <tr style = "border: 1px solid black;">
    <td ><%# Eval("final_amount") %></td>
    </tr>
    </table>
    </div>


    </table>


    <div style="height:305px; border: 1px solid black; width:250px; margin-left:805px; margin-top:-310px">
    <table style="border: 1px solid black; border-collapse: collapse;" >

    <tr style="border: 1px solid black;" >
    <th>Approve</th>
    <th>Reject</th>
    <th>Modify</th>
    </tr>
    <tr style="border: 1px solid black;">
    <td> <span style="margin-left:0px"><asp:Button ID="BT_submit2" runat="server" Text="Approve"
    OnClick="BT_submit11_Click" Height="35px" Width="80px" BackColor="LightGreen" ForeColor="White"
    Style="border-radius:8px" /></span></td>
    <%-- <td> <span style="margin-left:0px"><asp:Button ID="Button1" runat="server" Text="Reject"
    OnClick="BT_submit0_Click" Height="35px" Width="80px" BackColor="Red" ForeColor="White"
    Style="border-radius:8px" /></span></td>
    <td> <span style="margin-left:0px"><asp:Button ID="Button2" runat="server" Text="Modify"
    OnClick="BT_submit1_Click" Height="35px" Width="80px" BackColor="Blue" ForeColor="White"
    Style="border-radius:8px" /></span></td>--%>
    </tr>
    </table>
    </div>
    </div>

    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    </div>

    </asp:Content>

    source page where data not fill in textbox

    css

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;

    namespace WebApplication14
    {
    public partial class WebForm49 : System.Web.UI.Page
    {
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();


    public void EstablishConnection(string storeprocedure)
    {
    con.ConnectionString = connection;
    cmd.Connection = con;
    cmd.Connection.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = storeprocedure;
    }


    public void CloseConnection()
    {
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    con.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    txt3.Text = Request.QueryString["Benefited_leaves1"].ToString();

    }
    }
    }

    aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm49.aspx.cs" Inherits="WebApplication14.WebForm49" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server">

    <asp:TextBox runat="server" ID="txt3" Width="220px"></asp:TextBox>


    </asp:Content>

    Thursday, April 9, 2020 7:37 AM
  • User-719153870 posted

    Hi prabhjot1313,

    Since you provided the complete code, now we can find the reason for this "object reference not set to an instance" issue is that you passed nothing to this parameter Benefited_leaves1 in yout URL Response.Redirect("webform49.aspx?Benefited_leaves1 =" + bn2.Text); while the tostring() method in your source page's pageload event requires an object.

    You may wonder why i'm saying you passed nothing to this parameter when you literally add the bn2.Text to the URL, this i was wondering too but it finally turns out that there's a sapce between the parameter name and value just before the "=" in your URL: "webform49.aspx?Benefited_leaves1{here's the little space}=" + bn2.Text.

    Another thing is, i would suggest you to store your value in an asp.net control such as a label control so that you can easily get its value from server code. For example:

                                            <tr style="border: 1px solid black;">
                                                <td>Benefited Leaves</td>
                                                <td><%# Eval("Benefited_leaves") %></td>
                                                <td>
                                                    <asp:Label ID="bn2" runat="server" Text='<%# Eval("Benefited_leaves1") %>'></asp:Label>
                                                </td>
                                            </tr>

    And change your server code like below:

            protected void BT_submit11_Click(object sender, EventArgs e)
            {
                //GridViewRow gr = Gv2.SelectedRow;
                //Response.Redirect("webform49.aspx?Basic_allowance1 =" + gr.Cells[0].FindControl("Basic_allowance1").ToString());
    
                Button btn1 = (Button)sender;
                GridViewRow row = (GridViewRow)btn1.Parent.Parent;
                Label bn2 = (Label)row.FindControl("bn2");
                Response.Redirect("webform49.aspx?Benefited_leaves1=" + bn2.Text);
            }

    PS, if this helps solve your issue, please mark it as the answer. Thanks!

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2020 4:59 AM