Solved

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

Posted on 2009-07-06
11
798 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.
0
Comment
Question by:dimensionav
11 Comments
 
LVL 8

Expert Comment

by:MrAgile
Comment Utility
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
0
 
LVL 2

Expert Comment

by:nathanbaker
Comment Utility

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;

}
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
Comment Utility
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

0
 

Author Comment

by:dimensionav
Comment Utility
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

0
 

Author Comment

by:dimensionav
Comment Utility
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
just wondering if your other parameters should be Int as well...looks your BLL method takes all parameters of integer type
0
 

Author Comment

by:dimensionav
Comment Utility
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
0
 

Author Comment

by:dimensionav
Comment Utility
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
0
 

Author Comment

by:dimensionav
Comment Utility
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.
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
Comment Utility
Your BLL Method:
Public Shared Sub ActualizaEncabezadoDePedido(ByVal Id_Pedido As Integer, ByVal Observaciones As Integer, ByVal Estatus As Integer)

Your update Parameter:
    <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>

That is the type mismatch I think.
               
           
0
 

Author Comment

by:dimensionav
Comment Utility
What a novice I am !!!
I hope this won´t happen again.

Regards
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now