Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am attempting to get form values from a FormView in Visual Studio and submit them to an Access Database on a button click. I have tried SO MANY variations, but keep getting an error saying "No values for parameters..."

If I use a VB function with an insert command, do I also need an SQLData Source control? Please help!

Here is the code for my FormView:

HTML
<asp:FormView runat="server" ID="FacilityFormView" DataSourceID="SqlDataSource3" OnItemCommand="FacilityFormView_ItemCommand" DataKeyNames="SequenceNo,Source">

            <ItemTemplate>
               ...

           <p class="head9">Facility ID:</p>
    <div class='clear'></div>
            <asp:TextBox runat="server" ID="txtSeq"></asp:TextBox>
            <div class="clearB"></div>
             <p class="head9">Withdrawal Source:</p>
    <div class='clear'></div>
             <asp:DropDownList ID="ddlSource" runat="server" DataSourceID="SqlDataSource3" DataTextField="Source" DataValueField="Source">

                    </asp:DropDownList>
            <div class="clearB"></div>
           <p class="head9">Year:</p>
                <div class='clear'></div>
               <asp:DropDownList ID="ddlYear" runat="server">
                   <asp:ListItem Value="NA">Select a Year</asp:ListItem>
                   <asp:ListItem Value="2014">2014</asp:ListItem>
                   <asp:ListItem Value="2015">2015</asp:ListItem>
                   <asp:ListItem Value="2016">2016</asp:ListItem>
                   <asp:ListItem Value="2017">2017</asp:ListItem>
               </asp:DropDownList>

             ...

                 <asp:Button ID="btnFacility" runat="server" Text="Submit" CommandName="Insert" OnClick="btnFacility_Click" />
           </ItemTemplate>

    </asp:FormView>


Here are some of the methods I'm using in my VB file:
VB
   Public Event ItemCommand As FormViewCommandEventHandler

   Sub FacilityFormView_ItemCommand(ByVal sender As Object, ByVal e As FormViewCommandEventArgs)
       If e.CommandName = "Insert" Then

           FacilityFormView.ChangeMode(FormViewMode.Insert)


       End If

   End Sub

Protected Sub btnFacility_Click(sender As Object, e As System.EventArgs)

       Dim row As FormViewRow = FacilityFormView.Row

       Dim so As DropDownList = CType(row.FindControl("ddlSource"), DropDownList)
       Dim ye As DropDownList = CType(row.FindControl("ddlYear"), DropDownList)
       Dim un1 As DropDownList = CType(row.FindControl("ddlJan"), DropDownList)
       Dim un2 As DropDownList = CType(row.FindControl("ddlFeb"), DropDownList)
       Dim un3 As DropDownList = CType(row.FindControl("ddlMar"), DropDownList)

       Dim se As TextBox = CType(row.FindControl("txtSeq"), TextBox)
       Dim ja As TextBox = CType(row.FindControl("txtJan"), TextBox)
       Dim fe As TextBox = CType(row.FindControl("txtFeb"), TextBox)
       Dim ma As TextBox = CType(row.FindControl("txtMar"), TextBox)

       Dim cmd As New OleDbCommand("INSERT INTO Quarter1(SequenceNo, Source, strYear, January, Units1, February, Units2, March, Units3) VALUES (?,?,?,?,?,?,?,?,?)")

       cmd.Parameters.Add(New OleDbParameter("SequenceNo", se.Text))
       cmd.Parameters.Add(New OleDbParameter("Source", so.SelectedValue))
       cmd.Parameters.Add(New OleDbParameter("strYear", ye.SelectedValue))
       cmd.Parameters.Add(New OleDbParameter("January", ja.Text))
       cmd.Parameters.Add(New OleDbParameter("Units1", un1.SelectedValue))
       cmd.Parameters.Add(New OleDbParameter("February", fe.Text))
       cmd.Parameters.Add(New OleDbParameter("Units2", un2.SelectedValue))
       cmd.Parameters.Add(New OleDbParameter("March", ma.Text))
       cmd.Parameters.Add(New OleDbParameter("Units3", un3.SelectedValue))

   End Sub


Here is my data source control:
HTML
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ... SelectCommand="SELECT [UserName], [Facility], [SequenceNo], [Source] FROM [Query1a] WHERE ([UserName] = ?)" InsertCommand="INSERT INTO Quarter1(SequenceNo, Source, strYear, January, Units1, February, Units2, March, Units3) VALUES (?,?,?,?,?,?,?,?,?)">

         <SelectParameters>
            <asp:ProfileParameter Name="UserName" PropertyName="userName" Type="String" />
        </SelectParameters>

    </asp:SqlDataSource>

Thank you in advance!!!!

Liz
Posted
Updated 8-May-14 11:27am
v2
Comments
j snooze 8-May-14 17:02pm    
It looks like when you are grabbing a formview row, you are not telling it which row the Facility button was clicked on. You are instantiating a formview "row" object, but its not set to the one that the button was clicked on.

Dim row As FormViewRow = FacilityFormView.Row(
I think you can get that in the itemcommand event. Probably e.rowindex or e.index. Then if you create a method to "Save" or "Insert"

Private Sub Insert(RowIndex as integer)

call that in your itemcommand Insert(e.RowIndex)

Copy the code from your Facility button click and change that first line to
Dim row As FormViewRow = FacilityFormView.Row(RowIndex) or it may be
Dim row As FormViewRow = FacilityFormView.Rows(RowIndex)

This should point you in the right direction I think...or hope. I haven't tested any of this. All theory so far :).
j snooze 8-May-14 17:05pm    
A code view might look something like this.
Sub FacilityFormView_ItemCommand(ByVal sender As Object, ByVal e As FormViewCommandEventArgs)
If e.CommandName = "Insert" Then
InsertRow(e.RowIndex)
End If
End Sub

Private Sub InsertRow(RowIndex as Integer)
Dim row As FormViewRow = FacilityFormView.Row(RowIndex)

Dim so As DropDownList = CType(row.FindControl("ddlSource"), DropDownList)
Dim ye As DropDownList = CType(row.FindControl("ddlYear"), DropDownList)
Dim un1 As DropDownList = CType(row.FindControl("ddlJan"), DropDownList)
Dim un2 As DropDownList = CType(row.FindControl("ddlFeb"), DropDownList)
Dim un3 As DropDownList = CType(row.FindControl("ddlMar"), DropDownList)

Dim se As TextBox = CType(row.FindControl("txtSeq"), TextBox)
Dim ja As TextBox = CType(row.FindControl("txtJan"), TextBox)
Dim fe As TextBox = CType(row.FindControl("txtFeb"), TextBox)
Dim ma As TextBox = CType(row.FindControl("txtMar"), TextBox)

Dim cmd As New OleDbCommand("INSERT INTO Quarter1(SequenceNo, Source, strYear, January, Units1, February, Units2, March, Units3) VALUES (?,?,?,?,?,?,?,?,?)")

cmd.Parameters.Add(New OleDbParameter("SequenceNo", se.Text))
cmd.Parameters.Add(New OleDbParameter("Source", so.SelectedValue))
cmd.Parameters.Add(New OleDbParameter("strYear", ye.SelectedValue))
cmd.Parameters.Add(New OleDbParameter("January", ja.Text))
cmd.Parameters.Add(New OleDbParameter("Units1", un1.SelectedValue))
cmd.Parameters.Add(New OleDbParameter("February", fe.Text))
cmd.Parameters.Add(New OleDbParameter("Units2", un2.SelectedValue))
cmd.Parameters.Add(New OleDbParameter("March", ma.Text))
cmd.Parameters.Add(New OleDbParameter("Units3", un3.SelectedValue))

End Sub
Liz Heal 8-May-14 18:04pm    
Thanks for your help! Do I also need to keep my SQLdatasource control if I am using VB?
j snooze 9-May-14 9:05am    
No control is needed. The code you already have should do the trick.
j snooze 8-May-14 17:21pm    
oops, don't forget the cmd.execute after all the parameters...if you don't run the sql statement it will never work :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900