Zoro
Zoro

Reputation: 41

How to Display Database Data in TextBox?

I'm trying to display data from database in ASP.Net TextBox. But ASP.Net Textbox doesn't have DataSource and DataSourceID. I used ADO.Net Disconnect Approach to connect and retrieve data from MSSQL 2008 database. So how can I do that problem?

Upvotes: 0

Views: 9084

Answers (1)

Harv
Harv

Reputation: 523

This will get data from a table named "Table" which could be referenced numerically instead It will get data from the first row of the table (row 0) and it will get data from the first column (column 0) The column could be named if you want, or you could use one of the other six overloads.

txt.Text = (string)ds.Tables["Table"].Rows[0][0];

I would personally set a memory variable to the value and then assign it to the text box. And, I would check to see if there were any rows retrieved Something like this

string myValue;

if (ds.["Table"].Rows.Count > 0)
   {
      //You must cast the value because it is an object
       myValue = (string)ds.Tables["Table"].Rows[0][0];
   }
   else
   {
       myValue = "No Data found";
   }

   txt.Text = myValue;

Of course, if your are only retrieving one table, you could use a DataTable instead of a DataSet. A DataTable is "lighter" weight than a DataSet.

Hope this helps

Harvey Sather

Upvotes: 1

Related Questions