Results 1 to 17 of 17

Thread: Update All Values In Gridview Column

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Question Update All Values In Gridview Column

    hi all!!

    what i need to do is to fill a gridview With a query and just let one column
    to edit and when i click a button update all values in that column to the database...
    ex:

    table Answers:
    ID(Int)
    Question(Nvarchar(50)
    Answer(Bit)
    fill the gridview

    Select Question , Answer FROM Answers

    Ok now i need to edit all rows in the column Answer and if possible with enum

    answer = 0 Enum = Yes
    answer = 1 Enum = No
    answer = DbNull Enum = Not applicable or N/A

    anny ideas in how to do this??
    instead row to row?

    Thanks in advance

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Update All Values In Gridview Column


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Update All Values In Gridview Column

    thanks mendhak! saw the examples, but it still edit row by row!
    i need to change all values in the gridview column and then click update button
    to update all at once!

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Update All Values In Gridview Column

    So what you're looking for is Edit mode. You start off by creating an item template. The ItemTemplate obviously corresponds to a column, you already know that. You can however, create an EditItemTemplate. Just like the ItemTemplate, but it's a template meant exclusively for editing.

    You will then have a button external to the grid. User clicks it, set a class-level boolean to true (isInEditMode = true), and you should then handle the OnRowDataBound event. In there, I believe you can set the .Rowstate property of each row to Rowstate.Edit (something similar, intellisense will tell you).

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Update All Values In Gridview Column

    something like this?::

    Code:
    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem Value="TRUE">YES</asp:ListItem>
                            <asp:ListItem Value="FALSE">NO</asp:ListItem>
                            <asp:ListItem Value="NULL">N/A</asp:ListItem>
                        </asp:DropDownList>

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Update All Values In Gridview Column

    ok got it working!!!
    but not in the update button
    tis is what i got so far:

    Code:
           Dim conn As New SqlConnection
            Dim dgItem As GridViewRow
    
            For Each dgItem In GvLCRespostas.Rows
                Dim strQuery As String = "UPDATE LCRespostas SET Resposta = @Res"
                conn.ConnectionString = "Data Source=PORTLIS-RAS\SQLEXPRESS;Initial " & _
                            "Catalog=GesQual;Persist Security Info=True;User ID=Cleverops;Password=admin"
                conn.Open()
    
                Dim Command As New SqlCommand(strQuery, conn)
    dim str as string = "TRUE"
                Command.Parameters.AddWithValue("@Res", str)
                Command.ExecuteNonQuery()
                strQuery = String.Empty
                Command.Dispose()
            Next
    
            conn.Close()
    how to retrieve the value from my dropdownlist??
    Last edited by Motorui; Sep 11th, 2007 at 08:04 AM.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Update All Values In Gridview Column

    But the link provided gives a full example... using a datasource, creating the update methods and using the <EditItemTemplate>.

    Was it not useful?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Update All Values In Gridview Column

    yes mendhak i read the link, it helps but it only allows to edit one row at a time!
    what i need is to edit and update all rows at the same time!
    thanks again for your time!

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Update All Values In Gridview Column

    tried this but still not working!!
    Protected Sub BtnUpdate_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles BtnUpdate.Click

    Dim con As New SqlConnection
    Dim dgItem As GridViewRow
    Dim row As GridViewRow = GvLCRespostas.Rows(2)
    Dim comd As New SqlCommand

    con.ConnectionString = "Data Source=PORTLIS-RAS\SQLEXPRESS;Initial " & _
    "Catalog=GesQual;Persist Security Info=True;User ID=Cleverops;Password=admin"
    con.Open()

    For Each dgItem In GvLCRespostas.Rows

    comd.Connection = con

    comd.CommandText = "UPDATE LCRespostas SET Resposta = @Res"

    Dim list As DropDownList = CType(row.FindControl("DropDownList1"), DropDownList)

    If Not list Is Nothing Then

    Dim str As String = list.SelectedValue.ToString
    comd.Parameters.AddWithValue("@Res", str)
    comd.ExecuteScalar()

    End If

    Next
    con.Close()
    con.Dispose()
    'Response.Redirect("stockInput.aspx")

    End Sub
    gives the error :

    The variable name '@Res' has already been declared. Variable names must be unique within a query batch or stored procedure.

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Update All Values In Gridview Column

    UPDATE LCRespostas SET Resposta = @Res

    You need a WHERE clause in there as well.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Update All Values In Gridview Column

    ok Now it is giving an error that i don't know how to solve:

    Code:
            Dim StrOperador As Integer = DdlOperador.SelectedValue
            Dim StrUh As Integer = DdlUh.SelectedValue
            Dim StrData As Date = TxtData.Text
            Dim StrVoo As String = TxtVoo.Text
    
            Dim con As New SqlConnection
            Dim dgItem As GridViewRow
            Dim row As GridViewRow = GvLCRespostas.Rows(0)
            Dim comd As New SqlCommand
    
            con.ConnectionString = "Data Source=PORTLIS-RAS\SQLEXPRESS;Initial " & _
          "Catalog=GesQual;Persist Security Info=True;User ID=Cleverops;Password=admin"
            con.Open()
    
            For Each dgItem In GvLCRespostas.Rows
    
                comd.Connection = con
    
                comd.CommandText = "UPDATE ResLc SET IdRes = @Res " & _
                "FROM ResLc CROSS JOIN LControl WHERE " & _
                "(LControl.IdOp = @Op) AND (LControl.IdUH = @Uh) " & _
                "AND (LControl.Data = @Data) AND (LControl.Voo = @Voo)"
    
                Dim list As DropDownList = CType(row.FindControl("DropDownList1"), DropDownList)
    
                If Not list Is Nothing Then
    
                    Dim str As String = list.SelectedValue.ToString
                    comd.Parameters.AddWithValue("@Res", str)
                    comd.Parameters.AddWithValue("@Op", StrOperador)
                    comd.Parameters.AddWithValue("@UH", StrUh)
                    comd.Parameters.AddWithValue("@Data", StrData)
                    comd.Parameters.AddWithValue("@Voo", StrVoo)
    
    
                    comd.ExecuteScalar()
    
                End If
    
            Next
            con.Close()
            con.Dispose()
    The Error:

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index




    Line 86: Dim con As New SqlConnection
    Line 87: Dim dgItem As GridViewRow
    Line 88: Dim row As GridViewRow = GvLCRespostas.Rows(0)
    Line 89: Dim comd As New SqlCommand
    Line 90:

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Update All Values In Gridview Column

    hi again...
    searched trough the net and find examples but in c#
    as i am a noob in vb in C# i am even more:

    converted the code but I'm not been able to get it to work:

    Code:
        Private Sub Update()
    
            Dim sb As New StringBuilder()
    
            ' build the query
            For Each row As GridViewRow In GridView1.Rows
    
                sb.Append("UPDATE [ResLc] SET [IdPer] = '")
                sb.Append(TryCast(row.FindControl("DropDownList1"), DropDownList).Text)
                sb.Append("'")
                sb.Append(" WHERE [Id] = '")
                sb.Append(TryCast(row.FindControl("Pergunta"), TextBox).Text)
                sb.Append("'")
    
            Next
    
            Dim connectionString As String = (ConfigurationManager.ConnectionStrings _
                ("GesQualConnectionString").ConnectionString)
    
            Dim myConnection As New SqlConnection(connectionString)
    
            Dim myCommand As New SqlCommand(sb.ToString(), myConnection)
    
            myConnection.Open()
    
            myCommand.ExecuteNonQuery()
    
            myConnection.Close()
    
        End Sub
    gives the error:

    Object reference not set to an instance of an object.

    in this line:

    sb.Append(TryCast(row.FindControl("Pergunta"), TextBox).Text)

    any ideas?

  13. #13
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Update All Values In Gridview Column

    When you do:

    (row.FindControl("Pergunta")

    Not every row will have "pergunta" in it. For example, the header and footer. For each row, check its RowType property to see if it is equivalent to DataGridRowType.DataRow

    vb Code:
    1. For Each row As GridViewRow In GridView1.Rows
    2.  
    3.             If row.RowType = DataGridRowType.DataRow Then
    4.             sb.Append("UPDATE [ResLc] SET [IdPer] = '")
    5.             sb.Append(TryCast(row.FindControl("DropDownList1"), DropDownList).Text)
    6.             sb.Append("'")
    7.             sb.Append(" WHERE [Id] = '")
    8.             sb.Append(TryCast(row.FindControl("Pergunta"), TextBox).Text)
    9.             sb.Append("'")
    10.             End If
    11.         Next

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Update All Values In Gridview Column

    nop it isnt working also!

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Update All Values In Gridview Column

    Code:
    </asp:GridView>
        <asp:SqlDataSource ID="SqlDsResLc" runat="server"
            ConnectionString="<%$ ConnectionStrings:GesQualConnectionString %>" 
            SelectCommand="SELECT PerLC.Pergunta, Res.Res 
    FROM ResLc 
    LEFT OUTER JOIN Res ON ResLc.Id = Res.Id 
    LEFT OUTER JOIN PerLC ON ResLc.IdPer = PerLC.ID CROSS JOIN LControl 
    WHERE (LControl.IdOp = @Op) AND (LControl.IdUH = @Uh) AND (LControl.Data = @Data) AND (LControl.Voo = @Voo)"
            UpdateCommand="UPDATE ResLc SET IdRes = @Param1 WHERE (Id = @Id)" InsertCommand="INSERT INTO ResLc(IdRes, IdLc, IdPer) SELECT @IdRes AS Expr2, PerLC.ID, LControl.Id AS Expr1 FROM PerLC CROSS JOIN LControl WHERE (LControl.IdOp = @Op) AND (LControl.IdUH = @Uh) AND (LControl.Data = @Data) AND (LControl.Voo = @Voo)">
                   <UpdateParameters>
                       <asp:Parameter Name="Param1" />
                <asp:Parameter Name="Id" Type="Int32" />
                        </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="IdRes" />
                <asp:Parameter Name="Op" />
                <asp:Parameter Name="Uh" />
                <asp:Parameter Name="Data" />
                <asp:Parameter Name="Voo" />
            </InsertParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="DdlOperador" Name="Op" PropertyName="SelectedValue" />
                <asp:ControlParameter ControlID="DdlUh" Name="Uh" PropertyName="SelectedValue" />
                <asp:ControlParameter ControlID="TxtData" Name="Data" PropertyName="Text" />
                <asp:ControlParameter ControlID="TxtVoo" Name="Voo" PropertyName="Text" />
            </SelectParameters>
        </asp:SqlDataSource>
    even in the edit template it doesn't update the values!!!


    BUMP

  16. #16
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Update All Values In Gridview Column

    You haven't posted your gridview. Show us.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Update All Values In Gridview Column

    Code:
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDsResLc">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="Pergunta" HeaderText="Pergunta" SortExpression="Pergunta" />
                <asp:TemplateField HeaderText="Res" SortExpression="Res">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem Value="1">YES</asp:ListItem>
                            <asp:ListItem Value="2">NO</asp:ListItem>
                            <asp:ListItem Value="3">N/A</asp:ListItem>
                            <asp:ListItem></asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Res") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width