0

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?

3
  • What "data" are you trying to display, and can you not use the Text property of TextBox? Commented May 30, 2011 at 16:47
  • It's not... I mean...when retrieve data from database , we can connect with DataSource or DataSourceID (GridView,ListBox) such as... But Textbox doesn't have DataSourceID or DataSource.. When i trying to connect as below txt.Text=dataset.Table["Table"]; TextBox display "Table" Text... Commented May 30, 2011 at 17:07
  • That's because you have to tell your application what data is "interesting". You normally don't display a whole table in a textbox, but rather some specific information that you perhaps want to edit, and then do an update against the database again. Using a Dataset you can retrieve some cell from the table and display that, instead of all of it. That's why I asked, it's somewhat of an odd question to me.. :-/ Commented May 31, 2011 at 9:26

1 Answer 1

1

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

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

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.