Hello Friends
i need your help
i have 1 drop down list. and i fill it from database.(thought ADO.NET, SP)
now my next task is when user select any item (product name)from the drop drown list,then its number(product Code) will show on Label.
Please tell me about it
Thanks in advance
Loading
RujutaPosted Feb 19, 2010, 11:06 PM
DropDownList1.DataTextField = "ProductName";
DropDownList1.DataValueField = "ProductCode";
Now on DropDownList_SelectionChanged event
say
Label1.Text=DropDownList1.SelectedValue.Text;
kiran kumarPosted Feb 19, 2010, 9:46 PM
HERE IS THE CODE FOR YOU
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace labeldisplay
{
public partial class Form1 : Form
{
string s = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=KIRAN\\SQLEXPRESS;Initial Catalog=kiran;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select pname from pro", con);
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
comboBox1.Items.Add(dr["pname"].ToString());
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=KIRAN\\SQLEXPRESS;Initial Catalog=kiran;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select pcode from pro where pname='"+comboBox1.SelectedItem.ToString()+"'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
s = dr["pcode"].ToString();
}
label1.Text = s;
}
}
}
MARK THE ANSWER IF IT HELPS YOU