How to change data in grid after you change the SQL select for the data adapter

T

tomcarr1

I have an ASP.net webform with a datagrid on it.

I can change the SQL select statement for the grid in code by putting
a statement like this in the first line of Page_Load
OleDbDataAdapter1.SelectCommand.CommandText = "SELECT * from table
where ... "

The rest of the code in Sub Page_Load is

OleDbDataAdapter1.Fill(DsCategories1)
If Not IsPostBack Then
DataGrid1.DataBind()
End If

This works fine on the initial load of the the webform. If I want to
change the SQL command to load different data into the grid after the
form is loaded, and do that with a cmdButton, what code to I put under
the cmdButton to reload the webform or grid with the new sql select?
 
C

Cor Ligthert [MVP]

Tom,

There is one command you should do at almost every end of a program.

DataGrid1.DataBind()

Strange that you have told that this should only be done in the non post
back situation.

\\\
OleDbDataAdapter1.SelectCommand.CommandText = "SELECT * from table
where ... "

OleDbDataAdapter1.Fill(DsCategories1)
DataGrid1.DataBind()
///
Should do it in my opinion.

Cor
 
T

tomcarr1

Thanks for your help, but that does not work. Isn't there some kind
of refresh or reload command that reloads the whole page? I know there
must be, but I can't find it.
 
T

tomcarr1

One more piece of information. Part of my problem seems to be that the
dataset is not getting reloaded by the SQL statement, because if move
the value of a field in the dataset to a label, I get the old value,
not the new value from the new select.

OleDbDataAdapter1.SelectCommand.CommandText = "SELECT * from table
where ... "
Label1.Text = DsCategories1.Tables(0).Rows(0).Item(1)
 
C

Cor Ligthert [MVP]

Thanks for your help, but that does not work. Isn't there some kind
of refresh or reload command that reloads the whole page? I know there
must be, but I can't find it.

Assuming of course that you have also set the datagrid.datasource to the
newly gotten dataset which you have before the fill have created new or have
cleared. (I would create new because that goes faster), thhis all before you
do the databind.

Something as
\\\
OleDbDataAdapter1.SelectCommand.CommandText = "SELECT * from table
where ... "

DsCategories1 = New DsCatogeriesWhatever the class name is
'or
'DsCategories1.clear
OleDbDataAdapter1.Fill(DsCategories1)
mydatagrid.datasource = DsCategories1.tables(0).
DataGrid1.DataBind()
///


Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top