Results 1 to 4 of 4

Thread: [RESOLVED] Insert all items from gridview

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Resolved [RESOLVED] Insert all items from gridview

    Hello!

    I´m trying to create a add all button to execute a query and insert all rows from a gridview.

    Can't make it work, it only inserts one row.
    Help please.

    vb Code:
    1. Imports System.Data.SqlClient
    2.  
    3. Public Class page1
    4.     Inherits System.Web.UI.Page
    5.  
    6.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    7.  
    8.     End Sub
    9.  
    10.     Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    11.  
    12.         For Each row As GridViewRow In GridView1.Rows
    13.  
    14.             Dim rb As RadioButtonList = DirectCast(row.FindControl("RblRespostas"), RadioButtonList)
    15.             Dim resposta As String
    16.             resposta = rb.SelectedValue
    17.  
    18.             Dim cs = ConfigurationManager.ConnectionStrings("DbqualityCs").ConnectionString
    19.             Dim conn As New SqlConnection(cs)
    20.             Dim cmd As New SqlCommand()
    21.             cmd.Connection = conn
    22.             cmd.CommandText = "INSERT INTO respostas (id_pergunta, id_operador, id_uh, id_chklst," & _
    23.             " resposta, id_auditor, data, criador) VALUES " & _
    24.             "(@id_pergunta, @id_operador, @id_uh, @id_chklst, @resposta, @id_auditor, @data, @criador)"
    25.  
    26.             cmd.Parameters.Add("@id_pergunta", SqlDbType.Int).Value = "1"
    27.             cmd.Parameters.Add("@id_operador", SqlDbType.Int).Value = "1"
    28.             cmd.Parameters.Add("@id_uh", SqlDbType.Int).Value = "1"
    29.             cmd.Parameters.Add("@id_chklst", SqlDbType.Int).Value = "1"
    30.             cmd.Parameters.Add("@resposta", SqlDbType.Int).Value = resposta
    31.             cmd.Parameters.Add("@id_auditor", SqlDbType.Int).Value = "1"
    32.             cmd.Parameters.Add("@data", SqlDbType.DateTime).Value = "1-1-2011"
    33.             cmd.Parameters.Add("@criador", SqlDbType.NVarChar, 50).Value = "rasantos"
    34.  
    35.             cmd.Connection.Open()
    36.             cmd.ExecuteNonQuery()
    37.             cmd.Connection.Close()
    38.         Next
    39.  
    40.     End Sub
    41. End Class

    Code:
    <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="page1.aspx.vb" Inherits="pwquality.page1" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    
         <link type="text/css" href="css/ui-lightness/jquery-ui-1.8.14.custom.css" rel="stylesheet"/>
         <script src="scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
         <script src="scripts/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>
         <script type="text/javascript">
            $(function () {
                $("#TxtData").datepicker({ dateFormat: 'dd-mm-yy', gotoCurrent: true, showAnim: 'explode' });
            });
    	
    	 </script>
    
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <asp:SqlDataSource ID="SqlDsUh" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DbqualityCs %>" 
            SelectCommand="SELECT * FROM [uhandling]"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDsOp" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DbqualityCs %>" 
            SelectCommand="SELECT * FROM [operadores]"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDsAud" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DbqualityCs %>" 
            SelectCommand="SELECT * FROM [auditores]"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DbqualityCs %>" 
            SelectCommand="SELECT id, pergunta, question, id_chklst FROM perguntas
             WHERE (id_chklst = @id_chklst)" >
    
            <SelectParameters>
                <asp:QueryStringParameter Name="id_chklst" QueryStringField="ID" />
            </SelectParameters>
        </asp:SqlDataSource>
    
        <asp:table runat ="server" CellSpacing ="1" GridLines ="Both" >
        <asp:TableRow>
        <asp:TableHeaderCell>Unidade de Handling</asp:TableHeaderCell>
        <asp:TableHeaderCell>Operador</asp:TableHeaderCell>
        <asp:TableHeaderCell>Auditor</asp:TableHeaderCell>
        <asp:TableHeaderCell>Data</asp:TableHeaderCell>
        </asp:TableRow>
        <asp:TableRow>
        <asp:TableCell>
            <asp:DropDownList ID="DdlUh" runat="server" DataSourceID="SqlDsUh"
        appenddatabounditems="true" DataTextField="nome" DataValueField ="id">
            </asp:DropDownList>
        </asp:TableCell>
        <asp:TableCell>
            <asp:DropDownList ID="DdlOp" runat ="server" DataSourceID="SqlDsOp"
             appenddatabounditems="true" DataTextField ="nome" DataValueField="id">
             </asp:DropDownList>
        </asp:TableCell>
        <asp:TableCell >
            <asp:dropdownlist ID="DdlAud" runat="server"  DataSourceID ="SqlDsAud"
            appenddatabounditems="true"  DataTextField ="nome" DataValueField ="id">
            </asp:dropdownlist>
        </asp:TableCell>
        <asp:TableCell>
    <input type="text" id="TxtData"/>
        </asp:TableCell>
        </asp:TableRow>
            </asp:table>
            <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="id_chklst" HeaderText="id_chklst" 
                    SortExpression="id_chklst" Visible ="false" />
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                    ReadOnly="True" SortExpression="id" Visible ="false" />
                <asp:BoundField DataField="pergunta" HeaderText="pergunta" 
                    SortExpression="pergunta" />
                <asp:BoundField DataField="question" HeaderText="question" 
                    SortExpression="question" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:RadioButtonList ID="RblRespostas" runat="server">
                            <asp:ListItem Value="1">Sim</asp:ListItem>
                            <asp:ListItem Value="2">Não</asp:ListItem>
                            <asp:ListItem Value="3">N/A</asp:ListItem>
                        </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="Salvar" />
    </asp:Content>

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Insert all items from gridview

    Did you debug the code, Did the insert run for all the rows ?
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Insert all items from gridview

    Isn't Parameter.Add no longer recommended?

    I usually use Parameter.AddWithValue("@MyParam","My Value")

    Then you don't have to worry about type so much.

    --------------------------------------------------------------

    Now that aside, why are you inserting the same value in all datafields but one. I would assume your issue is you have 5 ID fields and you are inserting 1 into all of them. I'm assuming one is a unique field and you are throwing an exception after the first insert. Put a try...catch block around your cmd.ExecuteNonQuery() and put a break point on the catch and you're probably getting some kind of duplicate ID error from SQL.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    175

    Re: Insert all items from gridview

    Quote Originally Posted by SeanGrebey View Post
    Isn't Parameter.Add no longer recommended?

    I usually use Parameter.AddWithValue("@MyParam","My Value")

    Then you don't have to worry about type so much.

    --------------------------------------------------------------

    Now that aside, why are you inserting the same value in all datafields but one. I would assume your issue is you have 5 ID fields and you are inserting 1 into all of them. I'm assuming one is a unique field and you are throwing an exception after the first insert. Put a try...catch block around your cmd.ExecuteNonQuery() and put a break point on the catch and you're probably getting some kind of duplicate ID error from SQL.
    It is working now with:
    Code:
    cmd.Parameter.AddWithValue("@MyParam","My Value")
    about the same value im still working on it!
    I have a jquery datetimepicker that i need to pass the selected date to a string
    and some other fields from the gridview that are hidden

    hope i wont get problems with them.

    Thanks again

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