We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

dimensionav
dimensionav asked
on
Medium Priority
849 Views
Last Modified: 2012-05-07
HI

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.
Comment
Watch Question

Commented:
Hi There,

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

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_events.aspx

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 Engineer

Commented:
hi dimensionav,

               Check the useful links.  

Using Parameters with the SqlDataSource Control  :

http://msdn.microsoft.com/en-us/library/z72eefad%28VS.80%29.aspx

Updating, Deleting records using GridView control:

http://www.dotnetfunda.com/articles/article29.aspx

ObjectDataSource Control:

http://dotnetforum.lk/forums/p/3384/14053.aspx

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

http://www.dotnetcurry.com/ShowArticle.aspx?ID=147&AspxAutoDetectCookieSupport=1

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

http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx

Author

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"

Regards
' This is the gridview and datasource code
 
            <asp:GridView runat="server" 
                ID="gvPedidos" 
                ShowHeader="true"
                AutoGenerateColumns="False"
                RowStyle-CssClass="LineaDeContenidoCarrito" CellPadding="5" 
                AllowSorting="True" 
                DataSourceID="odsListadoPedidos" 
                DataKeyNames="Id_Pedido" 
                ShowFooter="true">
                <columns>
                    <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">
                        <EditItemTemplate>
                            <asp:TextBox id="tbObservaciones" TextMode="MultiLine" runat="server" Text='<%# Bind("Observaciones") %>' Width="150" Height="50"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbObservaciones" runat="server" Text='<%# Bind("Observaciones") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>                
 
                    <asp:TemplateField HeaderText="Estatus" SortExpression="Estatus" ItemStyle-Wrap="false" ItemStyle-Width="97" ItemStyle-HorizontalAlign="Right">
                        <EditItemTemplate>
                            <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:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbEstatus" runat="server" Text='<%# Bind("Estatus") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ButtonType="Button" ShowEditButton="true" ShowCancelButton="true" UpdateText="Actualizar" CancelText="Cancelar" />
                </columns>
 
                <RowStyle CssClass="LineaDeContenidoCarrito" />
                <FooterStyle CssClass="LineaDeTotalesCarrito" />  
            </asp:GridView>
 
           <asp:ObjectDataSource id="odsListadoPedidos" runat="server" 
                                  TypeName="DAV.TiendaEnLinea.v1.BusinessLogicLayer.BLLAdminEncabezadoPedidos" 
                                  SelectMethod="ObtieneListadoDePedidos" 
                                  UpdateMethod="ActualizaEncabezadoDePedido">
            <SelectParameters>
                <asp:querystringparameter name="FiltroPedidos" querystringfield="filtro" defaultvalue="0" Type="Int32" />
            </SelectParameters>
 
            <UpdateParameters>
                <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" />
            </UpdateParameters>
 
            </asp:ObjectDataSource>
 
 
'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)
 
                Try
                    Dim Tabla As DataTable = New DataTable
                    cmd.Fill(Tabla)
                    Tabla.AcceptChanges()
                    Tabla.Clear()
 
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
 
                End Try
            End Using
 
        End Function

Open in new window

Author

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.

Regards
Top Expert 2013

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

Author

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.

Regards


-047.jpg

Author

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

Regards.
BLLAdminEncabezadoDePedidos.txt
DALAdminEncabezadoDePedidos.txt
listado-pedidos.txt

Author

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.

Regards.
Top Expert 2013
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
What a novice I am !!!
I hope this won´t happen again.

Regards
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.