Please help me sir to remove mention 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 141: {
Line 142: GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
Line 143: string Flw_mtr = (row.FindControl("txtparam_dsc") as Label).Text;
Line 144: string param_dept = (row.FindControl("txtdeptt") as Label).Text;
Line 145: string rd_one = (row.FindControl("txtrdone") as TextBox).Text;
|
Source File: c:\Users\Administrator\Documents\Visual Studio 2012\Projects\ceo_dshbrd\ceo_dshbrd\home.aspx.cs Line: 143 Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ceo_dshbrd.home.Button2_Click(Object sender, EventArgs e) in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\ceo_dshbrd\ceo_dshbrd\home.aspx.cs:143
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9796650
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.4494.0
<asp:GridView ID="GridView1" runat="server" Width="1150px" EmptyDataText="***Data Not Found***"
AutoGenerateColumns="false" ShowFooter="true"
AutogeneratSelectButton="true"
HeaderStyle-BackColor="#4D4D4D"
HeaderStyle-ForeColor="White" Height="210px" >
<Columns>
<asp:TemplateField HeaderText="Flow meter" ItemStyle-Width="300">
<ItemTemplate>
<asp:label ID="txtparam_dsc" runat="server" Text='<%# Eval("Flw_mtr") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deptt." ItemStyle-Width="100">
<ItemTemplate>
<asp:label ID="txtdeptt" runat="server" Text='<%# Eval("param_dept") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reading at 00:00." ItemStyle-Width="150">
<ItemTemplate>
<asp:TextBox ID="txtrdone" runat="server" readonly="true" Text='<%# Eval("rd_one") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reading at 24:00" ItemStyle-Width="150">
<ItemTemplate>
<asp:TextBox ID="txtredtw" runat="server" Text='<%# Eval("rd_two") %>' onchange="Calulate(this)" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Difference" ItemStyle-Width="150">
<ItemTemplate>
<asp:TextBox ID="txtdiff" runat="server" Text='<%# Eval("diff") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks" ItemStyle-Width="150">
<ItemTemplate>
<asp:TextBox ID="txtparam_rmrk" runat="server" readonly="true" Text='<%# Eval("rmk") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
namespace ceo_dshbrd
{
public partial class home : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(@"Data Source=(localdb)\Projects;Initial Catalog=ceo_dhbd;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
protected void Page_Load(object sender, EventArgs e)
{
Label3.Text = System.DateTime.Now.ToString("dd-MM-yyyy");
string encodedDate = Request.QueryString["date"];
string selectedDate = HttpUtility.UrlDecode(encodedDate);
lblSelectedDate.Text = selectedDate;
if (!this.IsPostBack)
{
this.BindData();
if (Session["emp_id"] == null)
Response.Redirect("snsout.aspx");
else
{
String EmplooyeId = Session["emp_id"].ToString();
Label1.Text = " " + EmplooyeId + "";
String param_dept = Session["param_dept"].ToString();
Label2.Text = "" + param_dept + " ";
// String dept = Session["dept"].ToString();
//Label3.Text = "" + dept + " ";
}
}
}
protected void BindData()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Flw_mtr, param_dept,rmk,rd_one,rd_two,diff FROM dly_prm_dta1 where param_dept='" + Session["param_dept"] + "'And param_dt= '" + lblSelectedDate.Text + "'", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("caldr.aspx");
}
protected void Button2_Click(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
string Flw_mtr = (row.FindControl("txtparam_dsc") as Label).Text;
string param_dept = (row.FindControl("txtdeptt") as Label).Text;
string rd_one = (row.FindControl("txtrdone") as TextBox).Text;
string rd_two = (row.FindControl("txtredtw") as TextBox).Text;
string diff = (row.FindControl("txtdiff") as TextBox).Text;
string rmk = (row.FindControl("txtparam_rmrk") as TextBox).Text;
string entry_by = Label1.Text;
string entry_dt = Label3.Text;
string param_dt = lblSelectedDate.Text;
Object a;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
/* Check if data exists. If data exits, then update else insert */
using (SqlCommand cmd = new SqlCommand("select count(*) From dly_prm_dta1 where Flw_mtr = @Flw_mtr and param_dt = @param_dt"))
{
cmd.Parameters.AddWithValue("@Flw_mtr", Flw_mtr);
cmd.Parameters.AddWithValue("@param_dt", param_dt);
cmd.Connection = con;
con.Open();
a = cmd.ExecuteScalar();
con.Close();
}
SqlCommand cmd1;
if (a.ToString().Equals("1"))
{
cmd1 = new SqlCommand("update dly_prm_dta1 set rd_two = @rd_two ,diff = @diff,entry_by = @entry_by, entry_dt = @entry_dt where flw_mtr = @Flw_mtr and param_dt = @param_dt");
cmd1.Parameters.AddWithValue("@Flw_mtr", Flw_mtr);
cmd1.Parameters.AddWithValue("@rd_one", rd_one);
cmd1.Parameters.AddWithValue("@rd_two", rd_two);
cmd1.Parameters.AddWithValue("@diff", diff);
cmd1.Parameters.AddWithValue("@rmk", rmk);
cmd1.Parameters.AddWithValue("@entry_by", entry_by);
cmd1.Parameters.AddWithValue("@entry_dt", entry_dt);
cmd1.Parameters.AddWithValue("@param_dt", param_dt);
cmd1.Connection = con;
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
}
else
{
cmd1 = new SqlCommand("insert into dly_prm_dta1(Flw_mtr,param_dept,rd_one,rd_two,diff,rmk,entry_by,entry_dt,param_dt)values(@Flw_mtr,@param_dept,@rd_one,@rd_two,@diff,@rmk,@entry_by,@entry_dt,@param_dt)");
cmd1.Parameters.AddWithValue("@Flw_mtr", Flw_mtr);
cmd1.Parameters.AddWithValue("@param_dept", param_dept);
cmd1.Parameters.AddWithValue("@rd_one", rd_one);
cmd1.Parameters.AddWithValue("@rd_two", rd_two);
cmd1.Parameters.AddWithValue("@diff", diff);
cmd1.Parameters.AddWithValue("@rmk", rmk);
cmd1.Parameters.AddWithValue("@entry_by", entry_by);
cmd1.Parameters.AddWithValue("@entry_dt", entry_dt);
cmd1.Parameters.AddWithValue("@param_dt", param_dt);
cmd1.Connection = con;
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
}
}
}
}
}