Fill Bank Name
public void FillBankName()
{
SqlConnection con = new SqlConnection(@"Connection");
con.Open();
SqlCommand cmd = new SqlCommand("select Bank_id,Bank_name from Bank_master", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
Dictionary<string, string> test = new Dictionary<string, string>();
test.Add("select one", "select one");
for (int i = 0; i < dt.Rows.Count; i++)
{
test.Add(dt.Rows[i]["Bank_name"].ToString(), dt.Rows[i]["Bank_id"].ToString());
}
comboBox1.DataSource = new BindingSource(test, null);
comboBox1.ValueMember = "Value";
comboBox1.DisplayMember = "Key";
comboBox1.DataBindings.ToString();
con.Close();
}
Fill Bank Type
public void FillBankType()
{
SqlConnection con = new SqlConnection(@"Connection");
con.Open();
SqlCommand cmd = new SqlCommand("select id,Bank_Type from bank_type", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
Dictionary<string, string> test = new Dictionary<string, string>();
test.Add("select one", "0");
for (int i = 0; i < dt.Rows.Count; i++)
{
test.Add(dt.Rows[i]["Bank_Type"].ToString(), dt.Rows[i]["id"].ToString());
}
comboBox2.DataSource = new BindingSource(test, null);
comboBox2.ValueMember = "Value";
comboBox2.DisplayMember = "Key";
comboBox2.DataBindings.ToString();
con.Close();
}
Form load
private void Form1_Load(object sender, EventArgs e)
{
FillBankName();
FillBankType();
}
comboBox1_SelectedIndexChanged
string abc = comboBox1.SelectedValue.ToString();
if (abc != "[select one, select one]" && abc != "select one")
{
SqlConnection con = new SqlConnection(@"Connection");
con.Open();
SqlCommand cmd = new SqlCommand("select Bank_Number,Bank_Type from Bank_master where Bank_Id=@Bank_id", con);
cmd.Parameters.AddWithValue("@Bank_id", comboBox1.SelectedValue.ToString());
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
textBox1.Text = dt.Rows[0]["Bank_Number"].ToString();
comboBox2.SelectedValue = dt.Rows[0]["Bank_Type"].ToString().Trim();
}
else
{
MessageBox.Show("Data Not Found");
}
}