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

, which I am binding from code behind. The one of the columns in the
depends on
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();
GridView1.DataSource = dt;

The aspx code for the same is :

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

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