Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my task combobox is there.In this combo i have some itemcodes.Now i want to item names in textbox based on that item code.
I wrote this code but it is not working.Item code is integer value not a string.I tried all ways..

C#
private void cmbitemcode_SelectedIndexChanged(object sender, EventArgs e) 
{ 
if (cmbitemcode.SelectedValue=="101") 
{ 
txtitemname.Text = "Samosa"; 
} 
if (cmbitemcode.SelectedValue == "102") 
{ 
txtitemname.Text = "Cake"; 
} 
if (cmbitemcode.SelectedValue == "103") 
{ 
txtitemname.Text = "Biscuits"; 
} 
else 
{ 
} 
}
Posted
Updated 12-Apr-12 17:04pm
v2
Comments
Sergey Alexandrovich Kryukov 12-Apr-12 23:52pm    
Horrific "code". Do you really have items with string values "102", "103"..? Who needs them?
--SA

C#
private void cmbitemcode_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbitemcode.Text=="101")
{
txtitemname.Text = "Samosa";
}
if (cmbitemcode.Text== "102")
{
txtitemname.Text = "Cake";
}
if (cmbitemcode.Text== "103")
{
txtitemname.Text = "Biscuits";
}
else
{
}
}
 
Share this answer
 
Comments
Hari Krishna Prasad Inakoti 13-Apr-12 0:14am    
thanq nadixx
Clifford Nelson 13-Apr-12 1:29am    
You should have fixed the if's to else if's
You need to use the else if instead of just the if. What is happening is that you are going through all the conditsions when you do not have to:

C#
if (cmbitemcode.Text == "101")
{
    txtitemname.Text = "Samosa";
}
else if (cmbitemcode.Text == "102")
{
    txtitemname.Text = "Cake";
}
else if (cmbitemcode.Text == "103")
{
    txtitemname.Text = "Biscuits";
}
 
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