Hi ,
i have this grid view below :
<asp:GridView ID="GVAssetList" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" DataKeyNames="GVAssetList_ID">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
<Columns>
<asp:BoundField DataField="Asset_ID" HeaderText="Asset ID" />
<asp:BoundField DataField="Asset_name" HeaderText="Asset Name" />
<asp:BoundField DataField="Asset_SN" HeaderText="Asset SN" />
<asp:BoundField DataField="Make" HeaderText="Asset Make" />
<asp:BoundField DataField="Model" HeaderText="Asset Model" />
<asp:BoundField DataField="Emp_Name" HeaderText="Assigned to" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSelect" Text="Select" runat="server" OnClick="btnSelect_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and the code behind the page is like below :
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
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 GVAssetList_ID = Convert.ToInt64(GVAssetList.SelectedRow.Cells[0].Text);
Session["GVAssetList_ID"] = GVAssetList_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 GVAssetList_SelectedIndexChanged(object sender, EventArgs e)
//{
// Session["GvAssetList_ID"] = GVAssetList.SelectedDataKey.Value;
// try
// {
// SqlCommand cmd = con.CreateCommand();
// cmd.CommandText = "SP_GetAssetDetails";
// cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.AddWithValue("@Asset_ID", Convert.ToInt32(Session["GvAssetList_ID"]));
// con.Open();
// SqlDataReader My = cmd.ExecuteReader();
// if (My.Read())
// {
// txtAssetName.Text = My["Asset_Name"].ToString();
// txtAssetSN.Text = My["Asset_SN"].ToString();
// ddlMake.SelectedValue = My["Asset_Make"].ToString();
// ddlModel.SelectedValue = My["Asset_Model"].ToString();
// ddlEmpList.SelectedValue = My["Emp_ID_FK"].ToString();
// }
// con.Close();
// }
// catch
// {
// }
//}
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
{
}
}
}
when i run the page i get this error :
Server Error in '/' Application.
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'GVAssetList_ID'.
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.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'GVAssetList_ID'.
Source Error:
Line 32: sda.Fill(dt);
Line 33: GVAssetList.DataSource = dt;
Line 34: GVAssetList.DataBind();
Line 35: con.Close();
Line 36:
pleas help.
regards