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
Solved

Update multiple rows at once

Posted on 2011-02-20
8
646 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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