3

Which is the best control/approach in ASP.NET 4 to bind data from single record, which is fetched from sql server? I have some page to display text, so I need labels like title, date of publication, content and so on - single label for each data. Of course I can use ListView or Repeater, but I wonder if there's some other way.

Thanks in advance

3 Answers 3

2

You could use the DetailsView, which is designed to display a single record (though it's usually used in a master-details scenario). DetailsView can use SqlDatasource.

Trivial example:

<asp:SqlDataSource id="slqDataSource1" runat="server"
     ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
     SelectCommand="SELECT * FROM Table" />

<asp:DetailsView id="detailsView1" runat="server"
     AutoGenerateRows="true" DataKeyNames="Id"
     DataSourceID="sqlDataSource1" />

The above example creates a DetailsView based on the SelectCommnad of the SqlDataSource. In the implementation above, since AutoGenerateRows is set to true the control will render the data. However, you can also specify the fields explicitly, and have different fields you can choose from (BoundField, ButtonField, CheckBoxField, etc).

DetailsView Class

Sign up to request clarification or add additional context in comments.

Comments

2

sqldatareader is a good one.

you can create a sql-command, and then use the .executereader method. For example:

dim myReader as SqlDatareader
Dim myCommand As New SqlCommand("myStoredProcedure", myConnection)
        With myCommand
            .CommandType = CommandType.StoredProcedure
            With .Parameters
                .Clear()
                .AddWithValue("myParameter", parametervalue)
            End With
        End With
Try
   myConnection.open
   myReader = myCommand.ExecuteReader
   While myReader.Read
       Me.myTextBox.Text = myReader("fieldname")
       Me.myCheckbox.Checked = myReader("fieldname")
   End While
Catch ex As Exception
   Response.Write(ex.Message)
Finally
   myConnection.Close()
End Try

or some such...

4 Comments

OK, nice - but maybe something using SqlDataSource?
Usually, a SqlDatasource is used by data-bound controls (like gridview, listview, formview, ec.), which are usually are used for datasets with multiple records. Since you've only got one record, I figured you'd just be using labels and texboxes, etc., not databound controls. Anyway, short of using a databound control (which it sounds like you don't want to use), I think you'll need to use code-behind. And in that case, the solution above is probably your best. (You can use a sqldatasource in code-behind too, but it's a bit more convoluted than just using a sqldatareader as above).
OK, I understand. Btw, does change source mode to datareader may be more similar to your solution in terms of performance?
@deha -- I don't think performance is going to be an issue for you. You can, as the other fellow suggested, put this into it's own class (as opposed to the page class it might otherwise be in). How much overhead that saves you depends on a lot of things -- and in the end, either way will load the data faster than you can blink.
0

You can not bind data to a textfield. However, using a reader like the one listed above creates a WHOLE bunch of unneeded overhead. Why not create a class? This way you fetch it in your DAL and then do some processing (or conversion) in your BLL.

Be the way I would do it anyways. DataReaders and DataSets should not be used unless you are binding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.