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 two tables bankdetails and chequedetails table both tables have bankid.I created foreignkey on bankid column.I inserted some records in both the tables.Now i have bankname combobox,accountno textbox and accounttype combobox.And one datagridview.dridview is populate from the chequedetails table.
Now if i select bankname in the banknamecombobox accountno and account should displaying fine.Now i want based on banknameselectedid show the related row show in the datagridview
which is coming from chequedetails table.
Please help me on this.
Thanks in advance.

What I have tried:

<pre>   private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
            string abc = comboBox1.SelectedValue.ToString();
            if (abc != "[select, select]" && abc != "select")
            {
                SqlConnection con = new SqlConnection(connection);
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT [bank_Id],
           [bank_Accountno],bank_Accountype FROM bankdetails 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)
                {
                    txtAcntno.Text = dt.Rows[0]["bank_Accountno"].ToString();
                    cmbAcntype.Text = dt.Rows[0]["bank_Accountype"].ToString().Trim();
                    LoadDataGridView();
                }
            }
            else if (comboBox1.SelectedIndex == 0)
            {
                txtAcntno.Text = "";
                cmbAcntype.SelectedIndex = 0;
            }
        }



private void LoadDataGridView()
       {

       SqlConnection con = new SqlConnection(connection);
       SqlCommand cmd = new SqlCommand(" SELECT bank_Id,row_number() over(order by [bank_Id]) as SlNo,[bank_Chqstartno],[bank_Chqendno],case bank_Chqlvscount when 100 then 2 when 50 then 1 when 10 then 0 end as noofcheques ,bank_Chqlvscount,bank_Stat FROM [dbo].bankcheques where bank_Id=@bank_Id order by SlNo desc ", con);
       try
       {
           con.Open();
           SqlDataAdapter da = new SqlDataAdapter();
           da.SelectCommand = cmd;
           DataTable dt = new DataTable();
           da.Fill(dt);
           DataGridViewComboBoxColumn cmb = dgvwChqs.Columns["noofcheques"] as DataGridViewComboBoxColumn;
           cmb.DataSource = dt;
           cmb.DisplayMember = "bank_Chqlvscount";
           cmb.ValueMember = "bank_Id";
           cmb.HeaderText = "No. of Cheques";
           dgvwChqs.DataSource = dt;
           con.Close();
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message);
       }
   }
Posted
Updated 11-May-17 2:24am
v3
Comments
CHill60 11-May-17 7:45am    
Just introduce a WHERE clause into the SQL cmd in LoadDataGridView. You've already shown that you know how to do that. You could pass the selected bank as a parameter to that function if need be.
Member 13153537 11-May-17 8:12am    
bankname colum not there in chequedetails table.Only Bankid is there.could you guide me please how to do that.
Thanks in advance.
CHill60 11-May-17 8:18am    
According to your selectedindex changed code
cmd.Parameters.AddWithValue("@bank_Id", 
                 comboBox1.SelectedValue.ToString());
you already have the bankid
CHill60 11-May-17 8:19am    
If you use the  Reply  button I will be notified when you respond
Member 13153537 11-May-17 8:54am    
When i used this code atleast firsttime i am getting correct and then got an error."Object reference not set to instance of an object".
SqlCommand cmd = new SqlCommand(" SELECT bank_Id,row_number() over(order by [bank_Id]) as SlNo,[bank_Chqstartno],[bank_Chqendno],case bank_Chqlvscount when 100 then 2 when 50 then 1 when 10 then 0 end as noofcheques ,bank_Chqlvscount,bank_Stat FROM [dbo].bankcheques where bank_Id=@bank_Id order by SlNo desc ", con);

cmd.Parameters.AddWithValue("@bank_Id",
comboBox1.SelectedValue.ToString());

1 solution

Reading your question through again I'm guessing that you actually have the bank name as the selectedvalue in the ComboBox and not the bankid.

If you have the bank name and want to get the cheques based on that, then you just have to join to the bank details table ...
SQL
SELECT BC.bank_Id,row_number() over(order by [bank_Id]) as SlNo,[bank_Chqstartno],[bank_Chqendno],
	case bank_Chqlvscount when 100 then 2 when 50 then 1 when 10 then 0 end as noofcheques ,
	bank_Chqlvscount,bank_Stat 
FROM [dbo].bankcheques BC
INNER JOIN [dbo].bankdetails BD ON BC.bank_Id = BD.bank_Id
where bank_name=@bank_name 
order by SlNo desc 
 
Share this answer
 
Comments
Member 13153537 11-May-17 8:46am    
After Using the above query.i am getting empty dataset.
Member 13153537 11-May-17 8:47am    
After Using the above query.i am getting empty dataset.
CHill60 11-May-17 8:54am    
What is the value of the selectedvalue on the combo-box and do you have a bank with that name?
Member 13153537 11-May-17 9:03am    
based on the bank_Id. we are passing bank_Name column.
CHill60 11-May-17 9:12am    
That doesn't answer my questions! If you already have the bank_id then just search on that

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