Link to home
Start Free TrialLog in
Avatar of ANAT2403
ANAT2403Flag for Israel

asked on

update a gridview manually

In ASP.NET 2.0 and C# I have a page with a gridview and SqlDataSource.
In the beginning In SQLDatasource I had the commands for select and update.
I had to create the select manually with sql dataadapter and then bind it to the gridview because there was
aspecial query for it. so the selet command will not be from the sqldatasource. The question is if I can
still use the sqldatasource for the update command or I should also do it manually.
I so I would like to see an example for this.
thankYou
anat
ASKER CERTIFIED SOLUTION
Avatar of osiris247
osiris247
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ANAT2403

ASKER

But the updatecommand is relyed on the select fields isn't it?
ThankYou
Anat
no you can write whatever command you want.

like this.......
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connCleo %>"
                    SelectCommand="select chrforename + ' ' + chrsurname from cleo.users where iduser=1"
                    UpdateCommand="update cleo.usersloggedin set iduser=5">
</asp:SqlDataSource>
I created the sqldatasource with the updatecommand only, then in the code behind created the selectcommand
and put it together with
Sqldatasource1.SelectCommand = cmd.CommandText;
at the beginning there is no data and only after a GO button is pushed the select is created and the gridview
has values. The problem now is that when I want to update a row, The data in the gridview disappear , I get an empty gridview and only after I push the Go button again I get the option of updating a line and everything comes
back.
What is the problem?
Do you want the gridview to bind on load? If so put the databind in page_load event.

As for losing all data, have you got viewstate enabled?

Seve
I don't what the gridview to bind on load. Only after push the GO button.
Then I give the gridview the sql datasource with a select command but it dissappear  afterwords.
How do I enable the viewstate? Isn't in the default choice of a page?
Thanks
Anat
viewstate is a page and control property.  You will see it in the property window of the gridview and datasource.