Solved

Update multiple rows at once

Posted on 2011-02-20
8
648 Views
Last Modified: 2012-05-11
Asp.net 4

Hi

We currently have a web page with a listview control which is bound to a linq 2 sql datasource.

On each row of the list view there are textboxes and an update button which users must click to update one row at a time.

How could we could create a page on which there was multiple rows but only one update button on the page which would process all rows at once.

I guess we would use a repeater but has anybody done this?  Is linq 2 sql appropriate or is it better to use some other way to connect to sql server?

Wing
0
Comment
Question by:WingYip
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34940006
Hi,

try looking at this:

http://www.codeproject.com/KB/webforms/EditGridviewCells.aspx

its not quite what your after, but the edits are commited as you leave the cell, so NO submit button at all!

An alternate method would be to force the grid to display the data in textboxes, then parse the grid when submit is clicked to do all the updates. I prefer the editable grids, but let me know if you want me to explain further.

GMM
0
 
LVL 1

Author Comment

by:WingYip
ID: 34942091
Reason for the question is to get more work done on each round trip.
What is the alternate method you mention?
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34942141
Hi,

Ok posting from my mobile so can't be detaied but in essence what you'd need to do is

On the aspx page
Edit the gridview so that each column is a textbox (off the top of my head in vwd you click on the little tag to goto design modeand you can set it there)

 
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 5

Expert Comment

by:gingermoleman
ID: 34942208
Sorry, hit post without finishing!
In the code behind you'll need to parse your grid on the submit button

Something like
If gridview1.hasRows
For each row in gridview
Update table
Next
Endif
I'll be able to write out the actual code in about 4hrs if you need it

Gmm
0
 
LVL 5

Accepted Solution

by:
gingermoleman earned 500 total points
ID: 34946697
Hi,

the linq took a bit longer than expected but here you go.

On the gridview you get it to display in Textboxes by using item templates. the linq update seems to want the old and new value so we keep this in a label that we hide.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="LinqDataSource1">
<Columns>
<asp:TemplateField HeaderText="id">
<ItemTemplate>
<asp:label ID="lbid" runat="server" Width="25px"
 text='<%# Bind("id") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First">
<ItemTemplate>
<asp:Textbox ID="tbFirstName" runat="server" Width="100px"
 text='<%# Bind("firstName") %>'/>
<asp:label ID="hiddenFirstName" runat="server" Width="100px"
 text='<%# Bind("firstName") %>' Visible="false"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last">
<ItemTemplate>
<asp:Textbox ID="tbLastName" runat="server" Width="100px"
 text='<%# Bind("lastName") %>'/>
<asp:label ID="hiddenlastName" runat="server" Width="100px"
 text='<%# Bind("lastName") %>' Visible="false"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:LinqDataSource ID="LinqDataSource1" runat="server"
       ContextTypeName="updateAllDataContext" EnableUpdate="True" TableName="tests">
</asp:LinqDataSource>


in the code behind for a submit button, you need to parse the table one row at a time and update the record based on the ID


Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click

        Dim intRowIndex As Integer = 0
        Dim RowGrid As GridViewRow = GridView1.Rows(intRowIndex)
         For index As Integer = 0 To GridView1.Rows.Count - 1

            Dim keyValues As New ListDictionary()
            Dim newValues As New ListDictionary()
            Dim oldValues As New ListDictionary()
            keyValues.Add("id", Int32.Parse(CType(GridView1.Rows(index).FindControl("lbid"), Label).Text))

            oldValues.Add("firstname", CType(GridView1.Rows(index).FindControl("hiddenfirstname"), Label).Text)
            oldValues.Add("lastname", CType(GridView1.Rows(index).FindControl("hiddenlastname"), Label).Text)

            newValues.Add("firstname", CType(GridView1.Rows(index).FindControl("tbfirstname"), TextBox).Text)
            newValues.Add("lastname", CType(GridView1.Rows(index).FindControl("tblastname"), TextBox).Text)


            LinqDataSource1.Update(keyValues, newValues, oldValues)

            GridView1.DataBind()


        Next


    End Sub

Hope this helps

GMM
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34946714
hi again!

you dont need these 2 lines at all:

        Dim intRowIndex As Integer = 0
        Dim RowGrid As GridViewRow = GridView1.Rows(intRowIndex)


GMM
0
 
LVL 1

Author Comment

by:WingYip
ID: 35072630
Hi GMM

Sorry been away for a couple weeks and getting back to this now.

Interesting - need to mull this one over.

Thanks for the input.

REgards

Wing
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 35073391
Hi Wing,

No probs, let me know if theres any more questions

GMM
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question