TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sheesh Dubey
1.6k
116
9.7k
Error:Object reference not set to an instance of an object
Mar 9 2011 10:01 AM
Hi
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
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
AutoGenerateEditButton="True" CellPadding="4"
OnRowEditing="EditRec" OnRowUpdating="UpdateRec"
PageSize="5"
style="margin-left: 28px"
OnPageIndexChanging="ChangePage" OnSorting="SortRecords"
ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="Ename" HeaderText="Employee Name" />
<asp:BoundField DataField="Sal" HeaderText="Salary" />
<asp:BoundField DataField="Desig" HeaderText="Designation" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" BorderStyle="Groove" />
</asp:GridView>
Thanks
Regards
Reply
Answers (
4
)
storing image in db
how button event listen at runtime