• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 568
  • Last Modified:

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
0
ANAT2403
Asked:
ANAT2403
  • 4
  • 3
1 Solution
 
osiris247Commented:
You can put whatever commands into a sqldatasource you like.

aspx
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>"
         UpdateCommand="update table set field=value">
 </asp:SqlDataSource>

cs
SqlDataSource1.Update();

hope this helps
steve
0
 
ANAT2403Author Commented:
But the updatecommand is relyed on the select fields isn't it?
ThankYou
Anat
0
 
osiris247Commented:
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>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ANAT2403Author Commented:
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?
0
 
osiris247Commented:
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
0
 
ANAT2403Author Commented:
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
0
 
osiris247Commented:
viewstate is a page and control property.  You will see it in the property window of the gridview and datasource.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now