Click here to Skip to main content
15,877,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
when dropdownlist selected index changed am bringing a value from database and that value i wants to bind to a text box my code is(in this code am binding value to dropdownlist(ddlmonthlypay) how to bind that value to text box)
C#
protected void ddlNoOfMonths_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlmonthlypay.Items.Clear();
        ddlmonthlypay.AppendDataBoundItems = true;
        String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        String strQuery = "select ID, MonthlyPay from Tmonthlypay where NoOfMonthsID=@NoOfMonthsID";
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();

        cmd.Parameters.AddWithValue("@NoOfMonthsID", ddlNoOfMonths.SelectedItem.Value);
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQuery;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlmonthlypay.DataSource = cmd.ExecuteReader();
            ddlmonthlypay.DataTextField = "MonthlyPay";
            ddlmonthlypay.DataValueField = "ID";
            ddlmonthlypay.DataBind();
            if (ddlmonthlypay.Items.Count > 1)
            {
                ddlmonthlypay.Enabled = true;
            }
            else
            {
                ddlmonthlypay.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
           }
Posted
Updated 15-Jul-15 19:26pm
v2

Drop down list has separate properties for the text and the underlying value. Text box doesn't have this kind of feature. It only shows what is set to Text property. If you want, you can use Tag property to store desirend information concerning the text you have fetched from the database. Just set the value of the Tag.

Note that text box can't have multiple values so you just set the text and the tag so you don't bind teh object as you do with drop down list.
 
Share this answer
 
Comments
Member 11382784 16-Jul-15 2:43am    
am bringing only one value from database sir
and i got it sir
thank you
Wendelius 16-Jul-15 2:47am    
Glad you got it solved.
Hi,

you just put below code into your try...catch box

try
{
con.Opern();
SqlDataReader sqlRed = cmd.ExecuteReader();
while (sqlRed.Read())
{
       //replace your text box name with TextBox1
       TextBox1.Text = Convert.ToString(sqlRed["MonthlyPay"]);
}

......
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900