Small problem occur, while executing a update procedure in asp.net it shows object reference not set to an instance of an object, can't understand.
This is a small prog that have 3 tier architecture. The Stored procedure is perfectly executing have no problem.
The connection string it is perfectly right, because records are fetched from table to grid. On fixing the break point it throws the error in the Updaterec method. Morever in this statement
TextBox Tname = (TextBox)row.FindControl("Txtname");
the object tname shows null values & procedings objects also.
The compiler throws the error from catch statement.
Following is the code
protected void EditRec(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void BindGrid()
{
GridView1.DataSource = GridDatsource();
GridView1.DataBind();
}
private DataTable GridDatsource()
{
DataTable dtable = new DataTable();
try
{
dtable = B1.Loads();
}
catch (Exception ee)
{
throw (ee);
}
return dtable;
}
protected void UpdateRec(object sender, GridViewUpdateEventArgs gUe)
{
int res=0;
GridViewRow row = (GridViewRow)GridView1.Rows[gUe.RowIndex];
TextBox Tname = (TextBox)row.FindControl("Txtname");
TextBox Tsal = (TextBox)row.FindControl("TxtSal");
TextBox TDsg = (TextBox)row.FindControl("TxtDsg");
try
{
res = B1.Modi(Tname.Text, float.Parse(Tsal.Text), TDsg.Text);
if (res > 0)
Label6.Text = "Record Updated Successfully";
else
Label6.Text = "Record Not Updated";
}
catch (Exception EE)
{
Label7.Text = EE.Message.ToString();
}
finally
{
B1 = null;
}
}
protected void CancelRec(object sender, GridViewCancelEditEventHandler gCe)
{
GridView1.EditIndex = -1;
BindGrid();
}
Buisness Layer
public int Modi(String Emname, float Salary, String Desig)
{
try
{
return eDal.UpdateRec(Emname, Salary, Desig);
}
catch
{
throw;
}
finally
{
eDal = null;
}
}
public DataTable Loads()
{
try
{
return eDal.Load();
}
catch
{
throw;
}
finally
{
eDal = null;
}
}
Data Layer
public DataTable Load()
{
SqlConnection conn = new SqlConnection(Cns);
conn.Open();
SqlDataAdapter dAd = new SqlDataAdapter("UspDisplayRec", conn);
dAd.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet dSet = new DataSet();
try
{
dAd.Fill(dSet, "Employee");
return dSet.Tables["Employee"];
}
catch(Exception E)
{
throw E;
}
finally
{
dSet.Dispose();
dAd.Dispose();
conn.Close();
conn.Dispose();
}
}
public int UpdateRec(String Ename, float sal, String Desig)
{
SqlConnection conn = new SqlConnection(Cns);
conn.Open();
SqlCommand dCmd = new SqlCommand("UspModiRec", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("Ename", Ename);
dCmd.Parameters.AddWithValue("Sal", sal);
dCmd.Parameters.AddWithValue("Desig", Desig);
return dCmd.ExecuteNonQuery();
}
catch (Exception E)
{
throw E;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
Code
AutoGenerateEditButton="True" CellPadding="4"
OnRowEditing="EditRec" OnRowUpdating="UpdateRec"
PageSize="5"
style="margin-left: 28px"
OnPageIndexChanging="ChangePage" OnSorting="SortRecords"
ForeColor="#333333" GridLines="None">
Thanks
Regards
Sheesh DubeyPosted Mar 10, 2011, 9:57 AM
string Tname = gUe.NewValues["Ename"].ToString();
string Tsal = gUe.NewValues["Sal"].ToString();
string TDsg = gUe.NewValues["Dsg"].ToString();
then to the same error exists
Raja KrishnamurthyPosted Mar 10, 2011, 7:14 AM
string Tname = gUe.NewValues["Txtname"].ToString();//Not this
string Tname = gUe.NewValues["Ename"].ToString(); //this - DataFieldName and not control name
HTH
Happy Programming!!!
Regards,
Raja
Sheesh DubeyPosted Mar 9, 2011, 9:47 PM
When using e.NewValues["
then too it fires the same error. I am writing the code like this
Textbox Tname = gUe.NewValues["Txtname"].ToString();
it then fires the error that the string cannot be converted to textbox object. In this situation I have changed the object name Textbox tname to string tname & now the code is
string Tname = gUe.NewValues["Txtname"].ToString();
now it again fires the same error message Object reference not set to an instance of an object
Could you please write the code if the above code you find wrong
Thanks
Regards
Raja KrishnamurthyPosted Mar 9, 2011, 10:16 AM
http://stackoverflow.com/questions/1184563/asp-net-gridview-get-boundfield-value-in-code-behind-when-in-edit-mode
Instead of using FindControl you should use e.NewValues["
HTH.
Happy Programming!!!
Regards,
Raja