7

I'm trying to retrieve values using VB.NET from a SQL database. How do I use SqlDataSource.Select()? Is there a way to move the value to a variable that I can use for other things?

I know its kind of scattered and vague but that is the best I can do. I basically need to set a labels text to a value in a table.

3 Answers 3

4

This puts the result query in to a DataTable.

DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());
DataTable groupsTable = view.ToTable();
String value;

foreach (DataRow dr in dt.Rows)
{
    // Do something here IE grab the value of the first column
    value = dr[0];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, so I've added that but how do I get the data from the DataTable?
Err... ok, I'll go check it out :)
Why doesn't this work? ::: Label1.Text = groupsTable.Rows(0).Field("points")
4

Repying to last question in comment:

YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")

1 Comment

Thank you. I ended up figuring this out after a lot of stressful playing around with it. Thanks for following up on a question I asked to somebody else (who didn't follow up) :)
1

I was getting crazy trying to do this simple operation:

retrieving data from sqldatasource and put it into variables that I can manipulate.

At the end, Here the working behind code to do this for VB.NET:

Dim DV As New DataView()


Dim DataTable As New DataTable()

Dim SqlDataSource1 As New SqlDataSource()

Dim VALUE As String



SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Connection_name").ConnectionString


SqlDataSource1.SelectCommand = "SELECT * from Table"


DV = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)

DataTable = DV.ToTable()



For Each riga As DataRow In DataTable.Rows
    VALUE = riga("table_name").ToString


Next

the for each, in this case gets only the first value but you can get any value from datatable and put it into vector, or other strings, so you can control data coming from sqldatasource.

ENJOY

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.