locked
can gridview table format data fill to source page textboxes ? RRS feed

  • Question

  • User-1026236167 posted

    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:52 AM

Answers

  • User288213138 posted

    Hi prabhjot1313,

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

    <td id="bn2"><%# Eval("Benefited_leaves1") %></td>

    The Control.FindControl Method is searche the current naming container for the specified server control, and your bn2 is not a server control.

    You can change this <td> to Textbox control and then get its value.

     <asp:TextBox ID="bn2" runat="server" Text='<%#Eval("Benefited_leaves1") %>'></asp:TextBox>

    Response.Redirect("webform49.aspx?Benefited_leaves1 =" + bn2.Text);

    Also note that your "=" has a space in front of it.

    Best regards,

    Sam

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

All replies

  • User475983607 posted

    The null exception is the most common error in programming.  It means you are trying to work with an object that has not been initialized.  The error message always shows the line of code that caused the null exception.   

    The error must be coming from the following code because here are no TextBox server controls with the ID bn2.

    TextBox bn2 = (TextBox)row.FindControl("bn2");
    Response.Redirect("webform49.aspx?Benefited_leaves1 =" + bn2.Text);

    There is a <td> with the bn2 ID though.  

    <td>Benefited Leaves</td><td ><%# Eval("Benefited_leaves") %></td><td id="bn2"><%# Eval("Benefited_leaves1") %></td>

    Given your code, II recommend learning how to use the GridView control.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview?view=netframework-4.8

    Thursday, April 9, 2020 10:39 AM
  • User288213138 posted

    Hi prabhjot1313,

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

    <td id="bn2"><%# Eval("Benefited_leaves1") %></td>

    The Control.FindControl Method is searche the current naming container for the specified server control, and your bn2 is not a server control.

    You can change this <td> to Textbox control and then get its value.

     <asp:TextBox ID="bn2" runat="server" Text='<%#Eval("Benefited_leaves1") %>'></asp:TextBox>

    Response.Redirect("webform49.aspx?Benefited_leaves1 =" + bn2.Text);

    Also note that your "=" has a space in front of it.

    Best regards,

    Sam

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