locked
Saving in List view -VB.NET RRS feed

  • Question

  • User-1578974752 posted

    when I DEBUG->  For Each ri As ListViewItem In ListView1.Items ,It is skipping to Next.What is wrong here

    <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="QuestDetailID">

    <ItemTemplate>

    <div>

    <td align="left"><%#DataBinder.Eval(Container.DataItem, "Question") %></td>

    </div>

    <p>

    <tr style="background-color:White;">

    <tr><td align="left" colspan="3"> <asp:HiddenField ID="QuestionDetailID" runat="server" Value='<%#Eval("QuestDetailID") %>' /> </td></tr>

    <td align="left"> <asp:Label ID="Option1" runat="server" Text=' <%# Eval("Option1") %>'></asp:Label><asp:RadioButton ID="rad1" GroupName="Group1" runat="server" /></td>

    <td align="left"> <asp:Label ID="Option2" runat="server" Text=' <%# Eval("Option2") %>'></asp:Label> <asp:RadioButton ID="rad2" GroupName="Group1" runat="server" /></td>

    <td align="left"> <asp:Label ID="Option3" runat="server" Text=' <%# Eval("Option3") %>'></asp:Label> <asp:RadioButton ID="rad3" GroupName="Group1" runat="server" /></td>

    <td align="left"> <asp:Label ID="Label1" runat="server" Text=' <%# Eval("Option4") %>'></asp:Label> <asp:RadioButton ID="rad4" GroupName="Group1" runat="server" /></td>

    </tr>

    </ItemTemplate>

    </asp:ListView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:abcConnectionString %>" SelectCommand="SELECT [Question],[Option1], [Option2], [Option3], [Option4], [Option5], [QuestDetailID], [QuestionMasterID] FROM [QuestionDetail] WHERE ([QuestionMasterID] = @QuestionMasterID)">

    <SelectParameters>

    <asp:ControlParameter ControlID="qusetid" Name="QuestionMasterID" PropertyName="Text" Type="Int64" />

    </SelectParameters>

    </asp:SqlDataSource>

    CODE

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

      Dim sql As String = "insert into UserDetail(SelectedAnswer, QuestDetailID)values(@option,@id)"

    ' Dim sql As String = "update UserDetail Set SelectedAnswer=@Option where QuestDetailID=@id"

    For Each ri As ListViewItem In ListView1.Items

    ' For Each ri As ListViewDataItem In ListView1.Items

    If ri.ItemType = ListItemType.Item Then

     

    Dim r1 As RadioButton = TryCast(ri.FindControl("rad1"), RadioButton)

    Dim r2 As RadioButton = TryCast(ri.FindControl("rad2"), RadioButton)

    Dim r3 As RadioButton = TryCast(r2.FindControl("rad3"), RadioButton)

    Dim sqlparameterId As SqlParameter = New SqlParameter("id", SqlDbType.Int)

    'get the id of current questionDetail

    Dim questdetid As String = TryCast(ri.FindControl("QuestionDetailID"), HiddenField).Value

    sqlparameterId.Value = Convert.ToInt32(questdetid)

     

     

    If r1.Checked Then

    'if the first radio is checked,get text of option1 and set correctAnswer field to the text

    Dim answer As String = TryCast(r2.FindControl("Option1"), Label).Text

    Dim sqlParameterAnswer As SqlParameter = New SqlParameter("Option", SqlDbType.NVarChar, 100)

    sqlParameterAnswer.Value = answer

    ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)

    End If

    If r2.Checked Then

    'if the first radio is checked,get text of option2 and set correctAnswer field to the text

    Dim sqlParameterAnswer As SqlParameter = New SqlParameter("Option", SqlDbType.NVarChar, 100)

    Dim answer As String = TryCast(r2.FindControl("Option2"), Label).Text

    sqlParameterAnswer.Value = answer

    sqlParameterAnswer.Value = answer

    ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)

    End If

    If r3.Checked Then

    'if the first radio is checked,get text of option3 and set correctAnswer field to the text

    Dim sqlParameterAnswer As SqlParameter = New SqlParameter("Option", SqlDbType.NVarChar, 100)

    Dim answer As String = TryCast(r2.FindControl("Option3"), Label).Text

    sqlParameterAnswer.Value = answer

    sqlParameterAnswer.Value = answer

    ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)

    End If

    End If

    Next

     

    End Sub

    Private Sub SurroundingSub()

    'repeater.DataSource = SqlDataSource1.repeater.DataBind()

    For Each ri As ListViewDataItem In ListView1.Items

    Dim r1 As RadioButton = TryCast(ri.FindControl("rad1"), RadioButton)

    Dim r2 As RadioButton = TryCast(ri.FindControl("rad2"), RadioButton)

    Dim r3 As RadioButton = TryCast(ri.FindControl("rad3"), RadioButton)

    Dim r4 As RadioButton = TryCast(ri.FindControl("rad4"), RadioButton)

    Dim r5 As RadioButton = TryCast(ri.FindControl("rad5"), RadioButton)

    Next

    End Sub

    Thursday, August 30, 2018 3:57 AM

All replies

  • User1120430333 posted


    when I DEBUG-> For Each ri As ListViewItem In ListView1.Items ,It is skipping to Next.What is wrong here

    it's not clear as to what you are trying to do. An insert is not going to produce a list that can be iterated over using a loop. A loop in not going to do anything if there are no records in the list, it is just going to go to the next statement and leave the loop.

    Thursday, August 30, 2018 4:47 AM
  • User-1578974752 posted

    The Insertion is working fine when I use repeater.Now I changed to List view

    instead of

    For Each ri As RepeaterItem In repeater.Items

    If ri.ItemType = ListItemType.Item Or ri.ItemType = ListItemType.AlternatingItem Then

     

    To

      For Each ri As ListViewItem In ListView1.Items

    If ri.ItemType = ListItemType.Item Then    ->After this,the debugger is moving to Next all times

    Hence insertion is not working. How to solve that.Thanks

    Full working code for repeater is below:

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

      Dim sql As String = "insert into UserDetail(SelectedAnswer, QuestDetailID)values(@option,@id)"

    ' Dim sql As String = "update UserDetail Set SelectedAnswer=@Option where QuestDetailID=@id"

    For Each ri As RepeaterItem In repeater.Items

    If ri.ItemType = ListItemType.Item Or ri.ItemType = ListItemType.AlternatingItem Then

     

    Dim r1 As RadioButton = TryCast(ri.FindControl("rad1"), RadioButton)

    Dim r2 As RadioButton = TryCast(ri.FindControl("rad2"), RadioButton)

    Dim r3 As RadioButton = TryCast(r2.FindControl("rad3"), RadioButton)

    Dim sqlparameterId As SqlParameter = New SqlParameter("id", SqlDbType.Int)

    'get the id of current questionDetail

    Dim questdetid As String = TryCast(ri.FindControl("QuestionDetailID"), HiddenField).Value

    sqlparameterId.Value = Convert.ToInt32(questdetid)

     

     

    If r1.Checked Then

    'if the first radio is checked,get text of option1 and set correctAnswer field to the text

    Dim answer As String = TryCast(r2.FindControl("Option1"), Label).Text

    Dim sqlParameterAnswer As SqlParameter = New SqlParameter("Option", SqlDbType.NVarChar, 100)

    sqlParameterAnswer.Value = answer

    ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)

    End If

    If r2.Checked Then

    'if the first radio is checked,get text of option2 and set correctAnswer field to the text

    Dim sqlParameterAnswer As SqlParameter = New SqlParameter("Option", SqlDbType.NVarChar, 100)

    Dim answer As String = TryCast(r2.FindControl("Option2"), Label).Text

    sqlParameterAnswer.Value = answer

    sqlParameterAnswer.Value = answer

    ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)

    End If

    If r3.Checked Then

    'if the first radio is checked,get text of option3 and set correctAnswer field to the text

    Dim sqlParameterAnswer As SqlParameter = New SqlParameter("Option", SqlDbType.NVarChar, 100)

    Dim answer As String = TryCast(r2.FindControl("Option3"), Label).Text

    sqlParameterAnswer.Value = answer

    sqlParameterAnswer.Value = answer

    ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)

    End If

    End If

    Next

     Public Function ExcuteNunQuery(ByVal sql As String, ParamArray sqlParameters As SqlParameter()) As Integer
            Using con As SqlConnection = New SqlConnection("Data Source=10.170.10.26;Initial Catalog=abcTest;Persist Security Info=True;User ID=TEST;Password=Pass1234")

                Using com As SqlCommand = New SqlCommand(sql, con)
                    com.Parameters.AddRange(sqlParameters)
                    con.Open()
                    Return com.ExecuteNonQuery()
                End Using
            End Using
            con.Close()
        End Function

     Private Sub SurroundingSub()
            'repeater.DataSource = SqlDataSource1.repeater.DataBind()

            For Each ri As RepeaterItem In repeater.Items
                Dim r1 As RadioButton = TryCast(ri.FindControl("rad1"), RadioButton)
                Dim r2 As RadioButton = TryCast(ri.FindControl("rad2"), RadioButton)
                Dim r3 As RadioButton = TryCast(ri.FindControl("rad3"), RadioButton)
                Dim r4 As RadioButton = TryCast(ri.FindControl("rad4"), RadioButton)
                Dim r5 As RadioButton = TryCast(ri.FindControl("rad5"), RadioButton)
            Next
        End Sub

    Thursday, August 30, 2018 5:09 AM
  • User1120430333 posted

    For Each ri As ListViewItem In ListView1.Items
    If ri.ItemType = ListItemType.Item Then ->After this,the debugger is moving to Next all times
    Hence insertion is not working. How to solve that.Thanks

    if there is nothing in ListView1.Items, then it's not going to execute any code in the for loop.

    How do you know anything is in ListView1.Items?

    You can use the Visual Studio Debugger's  Quickwatch and look at what is inside ListView1,Items

    Friday, August 31, 2018 6:43 AM