开发者

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. 开发者_JAVA百科So how can I do that problem?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜