Archana B.R Archana B.R -4 years ago 103
ASP.NET (C#) Question

How to populated selected columns in GridView and update from code behind?

I have a

GridView1
, which I am binding from code behind. The one of the columns in the
GridView
depends on
Label1.Text
as follows:

SqlCommand comd = new SqlCommand("SELECT Location_Profile_Name, " + Label1.Text + " FROM Home_Profile_Master", con);
SqlDataAdapter da = new SqlDataAdapter(comd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();


The aspx code for the same is :

<asp:TemplateField HeaderText="Location_Profile_Name"
SortExpression="Location_Profile_Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("Location_Profile_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Home_Profile" SortExpression="Label10">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>


I am getting an error in the aspx page as: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Home_Profile'.

I am not able to figure out what the mistake is. Kindly Help...! thank you.

Answer Source

you missed 'Home_Profile' in the query.

SqlCommand comd = new SqlCommand("SELECT Location_Profile_Name," + Label1.Text + " as Home_Profile FROM Home_Profile_Master", con);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download