Discussion Locked

Databind in Text box ASP.Net

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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

3 replies

by nz_justice

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.

by franklin701

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());

by sanjuktin

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();
}

This discussion is closed to new replies.