Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Data source is an invalid type, it must be either an IListSource, IEnumerable or IDataSource.
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       
        string Sno = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtSno")).Text;
        string Name = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtName")).Text;
        string Age = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtage")).Text;
        string Address = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtaddress")).Text;

        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Details set Sno=@Sno, Name=@Name," +
     "Age=@Age where Sno=@Sno;" +
     "select Sno,Name,Age,Address from Details";

        cmd.Parameters.Add("@Sno", SqlDbType.Int).Value = Sno;
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
        cmd.Parameters.Add("@Age", SqlDbType.VarChar).Value = Age;
        cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = Address;

        GridView1.EditIndex = -1;
        GridView1.DataSource = (cmd);
        GridView1.DataBind();

When I run error occurs as follows Data source is an invalid type. It must be either an IListSource, IEnumerable or IDataSource.
In this line error occurs
C#
GridView1.DataSource = (cmd);


Please help me what is the problem form the above code I send.
Posted
Updated 30-Nov-12 0:59am
v2
Comments
Mendor81 30-Nov-12 7:04am    
http://www.codeproject.com/Questions/501502/Gridviewplusupdatingpluserror

Please don't double post the same question

you are assigning "cmd" to Gridview Datasource which is wrong
GridView1.DataSource = (cmd);
You have to assign datatbale to Gridview.
execute cmd and get data into datatable and assign to gridview
 
Share this answer
 
Hello ...


you can not assign gridview.datasource=(cmd);

you can assign dataset or datatable

GridView1.DataSource = ds;
or

GridView1.DataSource = dt;



depends on your fill method
 
Share this answer
 
v2
You are not executing your command cmd before assigning it to grid.
1) execute cmd and save it in datatable or dataset.
2) Bind dataset/datatable to grid
 
Share this answer
 

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