Web Development

Our forums are currently in maintenance mode and the ability to post is disabled. We will be back up and running as soon as possible. Thanks for your patience!

General discussion

Locked

Databind in Text box ASP.Net

By bsmgopal1984 ·
Dear all,
I used the below code for databind for dropdown list:It is working fine.
if(!IsPostBack)
{
ConnStr="Provider=msdaora;Data Source=inchndb;User Id=inchnasset;Password=inchn;";
Conn=new OleDbConnection(ConnStr);
cmd=new OleDbCommand("select primary_approver,Project from tbl_prj_mgr",Conn);
Conn.Open();
ddl_prj_mgr.DataSource=cmd.ExecuteReader();
ddl_prj_mgr.DataTextField="primary_approver";
ddl_prj_mgr.DataValueField="project";
ddl_prj_mgr.DataBind();
Conn.Close();
}
But now I want to get a string from database and to display in textbox.
I used the below code it is not working.
if(!IsPostBack)
{
ConnStr="Provider=msdaora;Data Source=inchndb;User Id=inchnasset;Password=inchn;";
Conn=new OleDbConnection(ConnStr);
cmd=new OleDbCommand("select empname from employee where manno='J0695'",Conn);
Conn.Open();
TextBox2.Text=cmd.ExecuteReader()Error comes here)
TextBox2.Text.DataBind();
Conn.Close();
}

*Error says that: Cannot implicitly convert type 'System.Data.OleDb.OleDbDataReader' to 'string'

So,how to get the value in textbox?
Thanks in advance

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

by NZ_Justice In reply to Databind in Text box ASP. ...

use tostring method to convert the data to a string then use the string to populate the box, sudoe code follows.

cmd.Executereader()tostring(strValue)As String

textbox2.text = strValue

so the idea is to add the data to a string then point the text box to the string.

Collapse -

Solutions Code

by franklin701 In reply to Databind in Text box ASP. ...

String ConnStr="Persist Security Info=False;User ID=dbuser1;Initial Catalog=WebShoppe;Data Source=j-53f";
SqlConnection Conn=new SqlConnection(ConnStr);
SqlCommand cmd=new SqlCommand("select InvID from OrderDetails",Conn);
Conn.Open();
//you need to Convert heree TextBox1.Text=Convert.ToString(cmd.ExecuteScalar());

Collapse -

Textbox binding in asp.net

by sanjuktin In reply to Databind in Text box ASP. ...

if(!IsPostBack)
{
ConnStr="Provider=msdaora;Data Source=inchndb;User Id=inchnasset;Password=inchn;";
Conn=new OleDbConnection(ConnStr);
cmd=new OleDbCommand("select empname from employee where manno='J0695'",Conn);
Conn.Open();
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
TextBox2.Text=dr[0].ToString();
}
Conn.Close();
}

Related Discussions

Related Forums