thank you for the quick reply
i did some changes , as i have asset_Id in my data table so changed the html code as below inside the grid view:
<asp:GridView ID="GVAssetList" runat="server" CellPadding="4" AutoGenerateColumns="false" DataKeyNames="Asset_ID">
and the code behind the page as below :
public partial class Admin_UpdateAssets : System.Web.UI.Page
{
#region Connection
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test_Connection"].ConnectionString);
#endregion Connection
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable("GVAssetList-dt");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "SP_GetAssetList";
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GVAssetList.DataSource = dt;
GVAssetList.DataBind();
con.Close();
}
}
protected void btnSelect_Click(object sender, EventArgs e)
{
// Int64 Asset_ID = Convert.ToInt64(GVAssetList.SelectedRow.Cells[0].Text);
// Session["GVAssetList_ID"] = Asset_ID;
ddlMake.DataBind();
GridViewRow SelectedRow = ((sender as Button).NamingContainer as GridViewRow);
pnlEditAsset.Visible = true;
lblAssetID.Text = SelectedRow.Cells[0].Text;
txtAssetName.Text = SelectedRow.Cells[1].Text;
txtAssetSN.Text = SelectedRow.Cells[2].Text;
ddlMake.ClearSelection();
ddlMake.Items.FindByText(SelectedRow.Cells[3].Text.Trim()).Selected = true;
//ddlModel.SelectedValue = SelectedRow.Cells[4].ToString();
//ddlEmpList.SelectedValue = SelectedRow.Cells[5].ToString();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
Session["GVAssetList_ID"] = GVAssetList.SelectedDataKey.Value;
try
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SP_EditAsset";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Asset_ID", Convert.ToInt32(Session["GVAssetList_ID"]));
cmd.Parameters.AddWithValue("@Asset_Name", txtAssetName.Text);
cmd.Parameters.AddWithValue("@Asset_SN", txtAssetSN.Text);
cmd.Parameters.AddWithValue("@Asset_Make", ddlMake.SelectedValue);
cmd.Parameters.AddWithValue("@Asset_Model", ddlModel.SelectedValue);
// cmd.Parameters.AddWithValue("@Emp_ID_FK", ddlEmpList.SelectedValue);
//cmd.Parameters.AddWithValue("@Asset_Status", ddlAssetStatus.SelectedValue);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
con.Close();
lblmsg.Text = "Asset Update!!";
GVAssetList.DataBind();
}
catch
{
}
}
}
and now when i want to run the update table code , i get this error below :
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 99: protected void btnUpdate_Click(object sender, EventArgs e)
Line 100: {
Line 101: Session["GVAssetList_ID"] = GVAssetList.SelectedDataKey.Value;
Line 102: try
Line 103: {
Source File: e:\MyTestWebSite\Admin\UpdateAssets.aspx.cs Line: 101
Stack Trace: