Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know I can drag/drop an sqlDataSource object onto my webform and then get my connection string from my webconfig file and link it and done... But I don't want to do it that way. I want to do it programmatically.

as it can be done in a windows form app like so:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionDev"].ConnectionString);
string command = "SELECT * FROM DUMMY";
SqlDataAdapter dataAdapter = new SqlDataAdapter(command, conn);

SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dgvDelete.DataSource = ds.Tables[0];

How can I do this in a web form?

I think I have to use the sqlDataSource object and set it up that way but I don't know how.

Can someone show me?

share|improve this question

2 Answers 2

If I understand the question you can create a function to return a DataTable which contains the data your want to view (or wherever you want to put the code). Then set the GridView control's datasource to your datatable then databind.

yourGridView.DataSource = yourDataTable

yourGridView.DataBind()

share|improve this answer

if you want see the data in the GridView control you must add this:

dgvDelete.DataBind();

For more details check this question

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.