Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have windows application.In that i have two forms one is bankaccount form and checkbook form. In Bank account form i have bankname textbox and accounttype dropdown list and account no textbox.after filling the data i can insert data to sql table.this is im done this.now my problem is in chquebook form bankname dropdown,accountno textbox and account type drop down.banking name is coming from database previously inserted.now i want if i select bankname automatically fill the accountno and accounttype from the datatable.I'm fresher please give me any idea.refer any link.Thanks in Advance.

What I have tried:

public void FillBankName()
        {
            DataRow dr;


            SqlConnection con = new SqlConnection(connection);
            con.Open();
            SqlCommand cmd = new SqlCommand("select bank_Id,bank_Name from bankdetails", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            dr = dt.NewRow();
            dr.ItemArray = new object[] { 0, "--Select--" };
            dt.Rows.InsertAt(dr, 0);

            comboBox1.ValueMember = "bank_Id";

            comboBox1.DisplayMember = "bank_Name";
            comboBox1.DataSource = dt;

            con.Close();
        }
Posted
Updated 8-May-17 20:43pm
Comments
Richard MacCutchan 8-May-17 6:20am    
You just need to extract the other details from the database, and display them in the relevant fields on the form.

Fill Bank Name
C#
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

C#
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

C#
private void Form1_Load(object sender, EventArgs e)
       {
           FillBankName();
           FillBankType();
       }


comboBox1_SelectedIndexChanged

C#
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");
               }
           }
 
Share this answer
 
Comments
Member 13153537 9-May-17 3:25am    
It is working fine for me firsttime.After loading form firsttime select is showing bankname. accountno and accounttype both are empty.when i select bankname both are filling fine.but after i select 'select' text again prevoiusly data showing in accountno and accounttype.In my table bankname,acountno and accounttype coming from single table only.
Thank very much for your effort.
C#
public void FillBankName()
        {
         //   DataRow dr;
 

            SqlConnection con = new SqlConnection(connection);
            con.Open();
            SqlCommand cmd = new SqlCommand("select bank_Id,bank_Name from bankdetails", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
 
           // dr = dt.NewRow();
           // dr.ItemArray = new object[] { 0, "--Select--" };
           // dt.Rows.InsertAt(dr, 0);
 
            comboBox1.ValueMember = "bank_Id";
 
            comboBox1.DisplayMember = "bank_Name";
            comboBox1.DataSource = dt;
            comboBox1.DataBindings.ToString();
            con.Close();
        }


 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connection);
            con.Open();
            SqlCommand cmd = new SqlCommand("select accountnumber,banktype from bank where bankid=@bankid", con);
            cmd.Parameters.AddWithValue("@bankid",comboBox1.SelectedValue);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            textBox1.Text=dt.Rows[0]["accountnumber"].ToString();
           
            SqlCommand cmd2 = new SqlCommand("select type_id,type_name from banktype ", con);
           
            SqlDataAdapter sda2 = new SqlDataAdapter(cmd2);
            DataTable dt2 = new DataTable();
            sda.Fill(dt2);

            comboBox2.DataSource = dt;
            comboBox2.ValueMember = "type_id";

            comboBox2.DisplayMember = "type_name";

            comboBox2.DataBindings.ToString();

            comboBox2.SelectedValue=dt.Rows[0]["banktype"].ToString();
            con.Close();
        }
 
Share this answer
 
v2
Comments
Member 13153537 8-May-17 6:01am    
Did you understand my question
Member 13153537 8-May-17 6:26am    
Thanks for your response.
shreyal acharya 8-May-17 6:29am    
Is this Solution is help you to Write your Code?
Member 13153537 6-Jun-17 5:39am    
Hi Please help me how to use the same code in class file and call it in front-end in different different.Thanks in advance.
Member 13153537 8-May-17 12:23pm    
Yes it is helpful for me.But when i run the program it is by default taking first bank name.Now i need to select after loading the form.How to do it any idea.

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