How to get values from a gridview control and pass them to a update parameters ?


Things are like this:
1. I have a gridview and objectdatasource controls.
2. The objectdatasource must get some update parameters in order to give them to the BLL

Those parameters have to get the values from a couple of controls in the gridview that are enclosed in EditItemTemplate tags.

How to that?

Thanks in advace.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi There,

Without seeing any of your code. You may be able to do this using the (RowCommand event).

There are few ways of doing it, depending on the event you use...
 here are a few examples...

protected void gridView1_SelectedIndexChanged(object sender, EventArgs e)

int Id = (int)gridView1.DataKeys[gridView1.SelectedIndex].Value;


protected void gridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

int Id = (int)gridView1.DataKeys[e.RowIndex].Value;

Tapan PattanaikSenior EngineerCommented:
hi dimensionav,

               Check the useful links.  

Using Parameters with the SqlDataSource Control  :

Updating, Deleting records using GridView control:

ObjectDataSource Control:

Pass Multiple Values from a GridView to Another Page using ASP.NET:

Simple Insert, Select, Edit, Update and Delete in Asp.Net GridView control:

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

dimensionavAuthor Commented:
HI Guys

I have followed the example of tapanpattanaik using FormParameter. According to the article has to get the fields from gridview controls. Please notice that the controls has values only during update method and one of them is a DropDownList.

I am getting the error "Input string was not in a correct format"

' This is the gridview and datasource code
            <asp:GridView runat="server" 
                RowStyle-CssClass="LineaDeContenidoCarrito" CellPadding="5" 
                    <asp:BoundField DataField="Id_Pedido" HeaderText="Pedido No." SortExpression="Pedido No." ItemStyle-Wrap="false" ItemStyle-Width="60" ReadOnly="true" ItemStyle-HorizontalAlign="center"></asp:BoundField>
                    <asp:BoundField DataField="Fecha" HeaderText="Fecha" SortExpression="Fecha" DataFormatString="{0:dd/MM/yyyy}" ItemStyle-Wrap="false" ItemStyle-Width="80" ReadOnly="true" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
                    <asp:BoundField DataField="Hora" HeaderText="Hora" DataFormatString="{0:t}" ItemStyle-Wrap="false" ItemStyle-Width="70" ReadOnly="true" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
                    <asp:BoundField DataField="Id_Cliente" HeaderText="Id Cliente" SortExpression="Id Cliente" ItemStyle-Wrap="false" ItemStyle-Width="70" ReadOnly="true" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
                    <asp:BoundField DataField="Id_Farrera" HeaderText="Id Farrera" SortExpression="Id Farrera" ItemStyle-Wrap="false" ItemStyle-Width="70" ReadOnly="true" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
                    <asp:BoundField DataField="Email" HeaderText="Email" ItemStyle-Wrap="false" ItemStyle-Width="206" ReadOnly="true" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
                    <asp:BoundField DataField="Nombre" HeaderText="Nombre" ItemStyle-Wrap="false" ItemStyle-Width="200" ReadOnly="true" ItemStyle-HorizontalAlign="Left"></asp:BoundField>
                    <asp:BoundField DataField="Plazo" HeaderText="Plazo" ItemStyle-Wrap="false" ItemStyle-Width="70" ReadOnly="true" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
                    <asp:TemplateField HeaderText="Observaciones" ItemStyle-HorizontalAlign="Center">
                            <asp:TextBox id="tbObservaciones" TextMode="MultiLine" runat="server" Text='<%# Bind("Observaciones") %>' Width="150" Height="50"></asp:TextBox>
                            <asp:Label ID="lbObservaciones" runat="server" Text='<%# Bind("Observaciones") %>'></asp:Label>
                    <asp:TemplateField HeaderText="Estatus" SortExpression="Estatus" ItemStyle-Wrap="false" ItemStyle-Width="97" ItemStyle-HorizontalAlign="Right">
                            <asp:DropDownList ID="ddlEstatus" runat="server" SelectedValue='<%# Bind("Estatus") %>'>
                                <asp:ListItem Value="PENDIENTE">PENDIENTE</asp:ListItem>
                                <asp:ListItem Value="COMPLETADO">COMPLETADO</asp:ListItem>
                                <asp:ListItem Value="CANCELADO">CANCELADO</asp:ListItem>
                            <asp:Label ID="lbEstatus" runat="server" Text='<%# Bind("Estatus") %>'></asp:Label>
                    <asp:CommandField ButtonType="Button" ShowEditButton="true" ShowCancelButton="true" UpdateText="Actualizar" CancelText="Cancelar" />
                <RowStyle CssClass="LineaDeContenidoCarrito" />
                <FooterStyle CssClass="LineaDeTotalesCarrito" />  
           <asp:ObjectDataSource id="odsListadoPedidos" runat="server" 
                <asp:querystringparameter name="FiltroPedidos" querystringfield="filtro" defaultvalue="0" Type="Int32" />
                <asp:FormParameter FormField="Id_Pedido" Name="Id_Pedido" Type="string" />
                <asp:FormParameter FormField="Observaciones" Name="Observaciones" Type="string" />
                <asp:FormParameter FormField="Estatus" Name="Estatus" Type="string" />
'This is the BLL method that catches the parameters
        Public Shared Sub ActualizaEncabezadoDePedido(ByVal Id_Pedido As Integer, ByVal Observaciones As Integer, ByVal Estatus As Integer)
            Dim objPedido As DAV.TiendaEnLinea.v1.DataAccessLayer.DALAdminEncabezadoDePedidos = New DAV.TiendaEnLinea.v1.DataAccessLayer.DALAdminEncabezadoDePedidos
            objPedido.ModificaEncabezadoDePedido(Id_Pedido, Observaciones, Estatus)
        End Sub
' This is the DAL Method that updates the data
        Public Function ModificaEncabezadoDePedido(ByVal idpedido As Integer, ByVal observaciones As Integer, ByVal estatus As Integer) As Integer
            Dim sql As String
            sql = "UPDATE(PEDIDOS) " & _
                  "SET OBSERVACIONES='" & observaciones & "', ESTATUS='" & estatus & "' " & _
                  "WHERE ID_PEDIDO=" & idpedido
            Using con As New OleDbConnection(_ConnectionString)
                Dim cmd As OleDbDataAdapter = New OleDbDataAdapter(sql, con)
                    Dim Tabla As DataTable = New DataTable
                Catch ex As Exception
                End Try
            End Using
        End Function

Open in new window

dimensionavAuthor Commented:
One parameter wasn´t typed correctly, this is the new value:
<asp:FormParameter FormField="Id_Pedido" Name="Id_Pedido" Type="int32" />

The error is still present.

just wondering if your other parameters should be Int as well...looks your BLL method takes all parameters of integer type
dimensionavAuthor Commented:
You´re Right

At this point BLL has one int parameter and two string ones. But the error keeps present.
I have attached the error image.


dimensionavAuthor Commented:
I have attached the files for better understanding.
Please change TXT extension according to the File Description details

dimensionavAuthor Commented:
What I have noticed is that the value contained in the textbox of tbObservaciones (During Edit Event) is considered as int32, according to this part of the error message:

[Exception: demo is not a valid value for Int32.] In which demo is the value captured during test.

Your BLL Method:
Public Shared Sub ActualizaEncabezadoDePedido(ByVal Id_Pedido As Integer, ByVal Observaciones As Integer, ByVal Estatus As Integer)

Your update Parameter:
                <asp:FormParameter FormField="Id_Pedido" Name="Id_Pedido" Type="string" />
                <asp:FormParameter FormField="Observaciones" Name="Observaciones" Type="string" />
                <asp:FormParameter FormField="Estatus" Name="Estatus" Type="string" />

That is the type mismatch I think.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dimensionavAuthor Commented:
What a novice I am !!!
I hope this won´t happen again.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.