How to Read Data from SqlDataSource During Load

85 Views Asked by At

I have a FormView that contains three radio buttons which have values of 1, 2, and 3 respectively. I need to bind the radio buttons to a database field in the FormView's datasource (I do not want to use a RadioList becuase I want to control the formatting, plus I want to know how to do this generally).

I figured out how to bind the data in the code behind to set the field values in the database. Now, I'd like to read the field value from the database to select which radio button is checked. I'm able to do this by running code during the page load that reads it from the database. However, is it possible to do this from the datas ource load event instead?

Html and FormView code:

<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="MyRecordID" DefaultMode="Edit" OnItemUpdating="FormView1_ItemUpdating" >
    <EditItemTemplate>
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />

        <label>Options:</label>
        <asp:RadioButton ID="RadioButton1" runat="server" GroupName="Test1" Text='1' />
        <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Test1" Text='2' />
        <asp:RadioButton ID="RadioButton3" runat="server" GroupName="Test1" Text='3' />
    </EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="MyConnString" 
    SelectCommand="SELECT * FROM MYTABLE WHERE MYRECORDID=1"
    UpdateCommand="UPDATE MYTABLE SET [MYRECORDOPTION]=@MyRecordOption WHERE MYRECORDID=1" >
    <UpdateParameters>
        <asp:parameter Type="Int32" Name="MyRecordOption" /> 
    </UpdateParameters>
</asp:SqlDataSource>

Code behind:

Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
'I could do something like this, but would prefer avoiding a database hit.
   
    Dim vRadioButton01 As RadioButton = FormView1.FindControl("RadioButton01")
    Dim vRadioButton02 As RadioButton = FormView1.FindControl("RadioButton02")
    Dim vRadioButton03 As RadioButton = FormView1.FindControl("RadioButton03")

    cn = New SqlConnection(db)
    cn.Open()

    sq = "SELECT * FROM [MYTABLE] WHERE MYRECORDID = 1"
    cm = New SqlCommand(sq, cn)
    dr = cm.ExecuteReader()

    If dr.Read() Then
        If dr("MyRecordOption") = 1 Then
            vRadioButton01.Checked = true
        ElseIf dr("MyRecordOption") = 2 Then
            vRadioButton02.Checked = true
        ElseIf dr("MyRecordOption") = 3 Then
            vRadioButton03.Checked = true
        End If
    End If

    dr.Close()
    cn.Close()
    dr = Nothing
    cm = Nothing
    cn = Nothing
End Sub

Protected Sub FormView1_ItemUpdating(ByVal Sender As Object, ByVal e As FormViewUpdateEventArgs) Handles FormView1.ItemUpdating
'Because I could not bind the radio buttons in the FormView, I'm manually binding them.

    Dim vRadioButton01 As RadioButton = FormView1.FindControl("RadioButton01")
    Dim vRadioButton02 As RadioButton = FormView1.FindControl("RadioButton02")
    Dim vRadioButton03 As RadioButton = FormView1.FindControl("RadioButton03")

    If vRadioButton01.Checked Then
        e.NewValues("MyRecordOption") = 1
    ElseIf vRadioButton02.Checked Then
        e.NewValues("MyRecordOption") = 2
    ElseIf vRadioButton03.Checked Then
        e.NewValues("MyRecordOption") = 3
    End If
End Sub


Private Sub SqlDataSource1_Load(sender As Object, e As EventArgs) Handles SqlDataSource1.Load
'Rather than reading from database. I'd like to utilize the Datasource since it's already reading it.
'Something similar to the below that works (this doesn't).

    'If e.value("MyRecordOption") = 1 Then
    '   vRadioButton01.Checked = true
    'ElseIf e.value("MyRecordOption") = 2 Then
    '   vRadioButton02.Checked = true
    'ElseIf e.value("MyRecordOption") = 3 Then
    '   vRadioButton03.Checked = true
    'End If
End Sub
0

There are 0 best solutions below