I have the following DetailsView, with several BoundFields, and SQlDataSource that populates the fields:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="TICKET_ID"
DataSourceID="SqlDataSource1" HeaderText="Completed IT ticket information"
CellPadding="4" ForeColor="#333333" GridLines="None" HorizontalAlign="Center">
<Fields>
<asp:BoundField DataField="TICKET_ID" SortExpression="TICKET_ID" Visible="False" />
<asp:BoundField DataField="SUBMITTED_BY" SortExpression="SUBMITTED_BY" Visible="False" />
<asp:BoundField DataField="TICKET_TITLE" HeaderText="Ticket Description" SortExpression="TICKET_TITLE" />
<asp:BoundField DataField="SUBMITTED_BY" HeaderText="Submitted By" SortExpression="SUBMITTED_BY" />
<asp:BoundField DataField="SOLUTION_NOTES" HeaderText="Solution Notes" SortExpression="SOLUTION_NOTES" />
<asp:BoundField DataField="EMAIL_HISTORY" HeaderText="Email History" SortExpression="EMAIL_HISTORY" />
<asp:BoundField DataField="COMPLETED_BY" HeaderText="Completed By" SortExpression="COMPLETED_BY" />
<asp:BoundField DataField="COMPLETE_DATE" HeaderText="Completed Date" ReadOnly="True" SortExpression="COMPLETE_DATE" />
</Fields>
</asp:DetailsView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TTPRODConnectionString %>"
SelectCommand="SELECT USR_ITFAC.TS_ID AS TICKET_ID, USR_ITFAC.TS_EC1_SUBMITTER AS SUBMITTED_BY, USR_ITFAC.TS_TITLE AS TICKET_TITLE, USR_ITFAC.TS_SOLUTION_NOTES AS SOLUTION_NOTES, USR_ITFAC.TS_EMAIL_HISTORY AS EMAIL_HISTORY, TS_USERS.TS_NAME AS COMPLETED_BY, DATEADD(HOUR,-8,USR_ITFAC.TS_CLOSEDATE) AS COMPLETE_DATE FROM USR_ITFAC INNER JOIN TS_USERS ON USR_ITFAC.TS_COMPLETED_BY = TS_USERS.TS_ID WHERE (USR_ITFAC.TS_ISSUEID = '00033')">
<SelectParameters>
<asp:QueryStringParameter Name="ts_id" QueryStringField="id" />
</SelectParameters>
</asp:SqlDataSource>
I hard-coded a value at the end of the query ‘00033’, which is the ID of a record I know is in the database. I tested the query and it returns a value as expected, what I’m trying to do is fetch the values of the BoundField in the code-behind after a user has pressed a button.
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Try
''Use a Dictionary to store answers to questions that were marked poor or fair
Dim answers As New Dictionary(Of String, String)
''For Each test
For Each dv_row As DetailsViewRow In DetailsView1.Rows
''Print rows data to console
Next
Catch ex As Exception
lblWarn.Text = "<br /><b>Please answer all the questions on this survey</b><br />"
'Response.Write(ex)
End Try
End Sub
Above I’m doing a test to fetch the values and print them onscreen, the problem is that the row count is 0, I’m not sure why that is. I expected the row count to be 8, when debugging I notice that the field count is 8, but I’m not sure how to get the values from the fields. I thought the way to get row data was something like:
Dim rowText As String = DetailsView1.Rows(0).Cells(1).Text
But when I try that I get a Null exception. Any insight is appreciated.
Was able to get it up and running. Turns out the BoundFields were not getting populated because the parameter in the QueryStringField, namely “id”, was not being specified in the request URL. To resolve this I first tried to rewrite the URL path on Page_Load, that didn’t work, so I just created a separate page with a hyperlink to the page I’m working on with a variable appended to the end of the URL which provides the QueryString for the SQLDataSource to reference. More on the QueryString property here.