Click here to Skip to main content
13,195,379 members (66,409 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Data source is an invalid type, it must be either an IListSource, IEnumerable or IDataSource.
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
GridView1.DataSource = (cmd);


Please help me what is the problem form the above code I send.
Posted 30-Nov-12 0:56am
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171019.1 | Last Updated 30 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100