Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the error is A field or property with the name 'userid' was not found on the selected data source.




C#
protected void grdUserMaster_rowdatabound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (TableCell cell in e.Row.Cells)
                {
                    DataTable dtdetails=new DataTable();
                    GridView grddetails = e.Row.FindControl("grddetails") as GridView;
                    string userid = (e.Row.Cells[1]).Text;
                    string query="select * from login where userid='"+userid+"'";
                    dtdetails = GetData(query);
                    grddetails.DataSource = ConvertColumnsAsRows(dtdetails);
                    grddetails.DataBind();
                    grddetails.HeaderRow.Visible = false;
                }
            }
        }

public DataTable ConvertColumnsAsRows(DataTable dt)
        {
            DataTable dtnew = new DataTable();
if (dt != null && dt.Rows.Count > 0)
            {
            for (int i = 0; i <= dt.Rows.Count; i++)
            {
                dtnew.Columns.Add(Convert.ToString(i));
            }
            DataRow dr;
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                dr = dtnew.NewRow();
                dr[0] = dt.Columns[j].ToString();
                for (int k = 1; k <= dt.Rows.Count; k++)
                    dr[k] = dt.Rows[k - 1][j];
                dtnew.Rows.Add(dr);
            }
}
            return dtnew;
        }
Posted
Updated 13-Oct-15 3:47am
v2
Comments
Member 12003400 13-Oct-15 9:35am    
put
if(dt != null && dt.Rows.Count > 0)
condition in ConvertColumnsAsRows
Member 11559407 13-Oct-15 9:43am    
this is the error i am getting it

A field or property with the name 'userid' was not found on the selected data source.
Richard Deeming 13-Oct-15 9:51am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Member 11559407 13-Oct-15 10:03am    
Ok so can u guide me how can i write the code for this error
[no name] 13-Oct-15 10:07am    
"userid" column is not present in grid grddetails. So you are getting column is not found. Please check ConvertColumnsAsRows() what are columns it is returning.

Don't use "select *". Use only required columns from table.
Secondly use parametrised query. Because someone vulnerable to your code with SQL Injection.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900