8

I have the following SqlDataSource and I want to convert it to DataView and read a column from it:

SELECT     
    dbo.Divisions.DivisionShortcut, 
    COUNT(DISTINCT dbo.UserQuiz.Username) AS [Number of Participants]
FROM         
    dbo.Divisions 
INNER JOIN
    dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode 
INNER JOIN
    dbo.UserQuiz ON dbo.employee.Username = dbo.UserQuiz.Username 
INNER JOIN
    dbo.Quiz ON dbo.UserQuiz.QuizID = dbo.Quiz.QuizID
WHERE     
    (dbo.Quiz.QuizID = @QuizID)
GROUP BY 
    dbo.Divisions.DivisionShortcut

This SqlDataSource lets the user enter the number of the Quiz, and it will retrieve the total number of participants in that quiz. I want to convert this SqlDataSource to a DataTable and read a column from it.

So how to do that?

2
  • 1
    What you have is a SQL statement, not a SqlDataSource. Do you have any code for the creation of your SqlDataSource that is not working? Commented Feb 26, 2012 at 16:42
  • Reading a columns is not the issue after conversion into datatable Commented Nov 16, 2012 at 7:37

2 Answers 2

19

To get datasource

SqlDataSource SqlDataSource1 = new SqlDataSource(connectionString, selectQuery);

After you got a data source say SqlDataSource1

DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView view = (DataView)SqlDataSource1.Select(args);
DataTable dt = view.ToTable();

Now you can read a column easily from this dt(DataTable) e.g.

int columnNumber = 0;
for(int i=0;i<dt.Rows.Count;i++)
{
    MessageBox.Show(dt.Rows[i][columnNumber].ToString());
    //Console.log(dt.Rows[i][columnNumber].ToString());
}

Reference

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

Comments

6

The SqlDataSource has a Select method which you can call to get back a DataView from your SqlDataSource - see the MSDN documentation on that.

SqlDataSource dataSource = new SqlDataSource(connectionString, selectSql);
DataView view = (DataView)dataSource.Select(args);

DataTable table = view.ToTable();

Is that what you're looking for??

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.