Monday, March 26, 2012

fetch the value from each cell into datatable

I'm trying this code but nothing is being displayed

SqlConnection conn =newSqlConnection(ConfigurationManager.AppSettings["STRING_CON"]);

SqlDataAdapter da =newSqlDataAdapter("my_sproc", conn);

DataTable dt =newDataTable();

DataRow dr;

//Adding the columns to the datatable

dt.Columns.Add("CategoryIdIn");

dt.Columns.Add("CategoryNameVc");

foreach (DataGridItem itemin gd_freq.Items)

{

dr = dt.NewRow();

dr[0] = item.Cells[0].Text;

dr[1] = item.Cells[1].Text;

dt.Rows.Add(dr);

}//ForEach

da.Fill(dt);

gd_freq.DataSource = dt;

gd_freq.DataBind();

I am not clear what you are trying to do here.

Do you want to populate the datagrid by binding it to the datatable returned by my_sproc?

"fetch the value from each cell into datatable"

Where did the values in the cells come from?

Can you explain a bit more?

|||

Hi Robert,

I'm also a little lost after reading your posted code. It seems that first you filled a table by fetching the data from a data grid and then you called a stored procedure and updated that table with data returned. Note here that all the data you got from the data grid will be overridden now. So, what exactly are you going to do?

If you just want to fetch the data from a data grid and then update the data source, you can try to use da.Update(dt) in your code (instead of da.Fill);

And if you want to fill the data grid with data from the database, first, you can filldt by executing da.Fill(dt), then you can set gd_freq.datasource=dt. Finally call gd_freq.databind and by now you will have your data grid well populated.

Hope my suggestion could help

Thanks.

No comments:

Post a Comment