Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
There is table Employee which contains Name,Salary,Address,....
Now, When I select Name of Employee in dropdownlist then I want to show his Salary Automatically in Textbox...
How I do this..
Please give me solution...

Sorry For All yours inconvenience..

What I have tried:

C#
public partial class Maintanance : System.Web.UI.Page
{
    SqlConnection cn;
    SqlCommand cmd;
    SqlDataReader rd;
    Int32 value;

    protected void Page_Load(object sender, EventArgs e)
    {
        cn= new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True;Pooling=False");
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        if(DropDownList1.SelectedIndex!=0)
        {
            
            cmd = new SqlCommand("Select Salary from Employees where Empname='"+DropDownList1.SelectedValue.ToString()+"'", cn);
            cn.Open();
            value =(Int32)cmd.ExecuteScalar();
            cn.Close();
            TextBox1.Text = value;

        }
    }

    } 




Name - DropDownlist Here (When Name of Employee is Selected)
Available Leaves - Textbox here (I want to show his salary automatically in textbox)
Posted
Updated 17-May-16 6:51am
v8
Comments
Sergey Alexandrovich Kryukov 10-May-16 8:43am    
Not clear? What's the problem? What do you want to achieve?
—SA
Karthik_Mahalingam 10-May-16 8:45am    
question not clear, Provide more information by clicking Imporve question
Patrice T 10-May-16 15:38pm    
What is the problem ?
Keep in mind that we don't know what you talk about, give proper explanations. Your question is actually unclear.
Use Improve question to update your question.

1 solution

I'm not sure why you use the TextBox.TextChanged event. Shouldn't you use the ListControl.SelectedIndexChanged Event (System.Web.UI.WebControls)[^]?

And as Richard Deeming said in the comment, use a parameterized query instead.

C#
private string connectionString = ???;

void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedIndex > -1)
    {
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand("SELECT Salary FROM Employees WHERE Empname = @empName", conn);
            
            cmd.Parameters.AddWithValue("@empName", DropDownList1.SelectedValue.ToString());

            conn.Open();
            int value = (int)cmd.ExecuteScalar();
            TextBox1.Text = value.ToString();
        } // The connection automatically closed here because Dispose is called
    }
}
 
Share this answer
 
v5
Comments
Member 12305778 19-May-16 4:36am    
Hello sir,
I will use parameterized query now.
But,How I will get Salary in Textbox according to your answer.
George Jonsson 19-May-16 19:28pm    
Thought you could fill in the blanks.
See my updated answer.
Member 12305778 23-May-16 3:15am    
Error 1 Cannot implicitly convert type 'System.Data.SqlClient.SqlCommand' to 'System.Data.SqlClient.SqlConnection'

This error is shown Sir....If your code is used.
George Jonsson 23-May-16 3:30am    
Remove the line
cmd.Connection = cmd;
Sorry about that. See updated answer.
Now it is time to use your own programming skills.
Member 12305778 25-May-16 14:00pm    
Not giving any error
But value is also not shown in textbox ...:(

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