hcemp
hcemp

Reputation: 119

how can i fill the gridview with dataset?

this is my code :

    string aaa;
    aaa = Request.Params["aaa"];
    string strSel;
    if (aaa != "" && aaa != null)
    {
       // Response.Write("<script>alert('" + aaa + "');</script>");
        strSel = "Select * from inout where FName like '%" + aaa + "%'";

    }
    else
    {
        strSel = "Select * from inout";
    }
    Response.Write(strSel);
    string strProvider = "Server=(local);DataBase=AIS20060712101417;UID=sa;PWD=";
    Data0 ds= new Data0();
    SqlConnection MyConn = new SqlConnection(strProvider);
    MyConn.Open();
    SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
    MyAdapter.Fill(ds,"inout");  
    GridView1.DataSource = ds;
    GridView1.DataBind();  

then the gridview is empty.

Upvotes: 0

Views: 13503

Answers (3)

Denis Palnitsky
Denis Palnitsky

Reputation: 18387

Is AutoGenerateColumns set to true ?

Try adding ds.Tables[0] as DataSource.

Upvotes: 3

Darren Hoehna
Darren Hoehna

Reputation: 379

I had the same problem. My solution was to remove the "*" and manually include the column names.

Upvotes: 0

Abdul
Abdul

Reputation: 854

It's easy dear......Try this one it works well for me

 using (DataSet ds = new DataSet())
    {
        DataTable dt = new DataTable();
        ds.Tables.Add(dt);
        string str = "User ID=username;Password=password;Data Source=Test";
        OracleConnection conn = new OracleConnection(str);
        conn.Open();
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select * from table_name";
        cmd.CommandType = CommandType.Text;
        OracleDataAdapter da = new OracleDataAdapter(cmd); 
        da.Fill(dt);
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }

And it's Done.Happy Coding with C#

Upvotes: 1

Related Questions