Click here to Skip to main content
15,883,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a windows application form where I have three controls..

Combobox1 --- cmbNameS
Combobox1 ---- cmbVersion
TextBox1 ---- txtKeys

I have a database table Keys where i have softwarename, version and keys as columns and some values.

Now I want that when the form is loaded Combobox should show the distinct values(I have done that part) and based on what I select the Versions should be loaded automatically into Combobox2 and based on the selected version the key should be displayed in the Textbox...

My code...
C#
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 CMb
{
    public partial class Form1 : Form
    {
        SqlConnection connect = new SqlConnection("Data Source=Admin;Initial Catalog=Practice_DatabaseSQL;User Id=sa;password=p@ssw0rd");
        DataTable dt1;
        public Form1()
        {
            InitializeComponent();
            bmbBind();
        }
        
        void bmbBind()
        {
            connect.Open();
            SqlCommand cmd = new SqlCommand("SELECT Distinct SoftwareName from dbo.Keys", connect);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            
            if (dt.Rows.Count > 0)
            {
                cmbNameS.DataSource = dt;
                cmbNameS.DisplayMember = "SoftwareName";
                cmbNameS.ValueMember = "SoftwareName";
            }
        }

    }
}


I want some help to do the same....
Posted

1 solution

Put some code into the SelectedIndexChanged event of cmbNameS to query for the appropriate data based on the cmbNameS.SelectedItem, then just bind the results to cmbVersion in the same way that you have done it for cmbNameS.

To display the selected version in the text box you will need code in the SelectedIndexChanged event of cmbVersion that just assigns the SelectedItem.ToString()

[EDIT - further information in response to OP Comment - "unable to get desired result in textbox"]

This is the code you are using to populate the text box
private void cmbVersion_SelectedIndexChanged(object sender, EventArgs e)
{
    string query = "Select SKeys from dbo.Keys where SoftwareName='" + cmbVersion.SelectedValue + "'";
    SqlCommand cmd = new SqlCommand(query, connect);
    SqlDataReader data = cmd.ExecuteReader();

    if (data.Read())
    {
        txtKeys.Text = data.GetSqlValue(0).ToString();
    }

}

If you were to debug this you would find that your query string is incorrect or, as when I debug this, no error is displayed but processing aborts on the line
SQL
string query = "Select SKeys from dbo.Keys where SoftwareName='" + cmbVersion.SelectedValue + "'";

Change the line to
string query = "Select SKeys from dbo.Keys where SoftwareName='" + cmbVersion.SelectedItem.ToString() + "'";
and you should find that it works. (I did say SelectedItem in my original solution!)

I recommend that you do some research on simple debugging techniques - it took me seconds to find the problem once I had the code in the IDE. This CodeProject article Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] should get you going
 
Share this answer
 
v2
Comments
DevParashar 14-May-14 14:55pm    
OK this is the code that i added but is not working..

private void cmbNameS_SelectedIndexChanged(object sender, EventArgs e)
{

string query = "Select VersionNo from dbo.Keys where SoftwareName='" + cmbNameS.SelectedValue + "'";
SqlCommand com = new SqlCommand(query, connect);
SqlDataReader dr = com.ExecuteReader();

if (dr.Read())
{
txtKeys.Text = dr["Key"].ToString();
}
}
CHill60 14-May-14 15:02pm    
What is the error?
DevParashar 15-May-14 2:40am    
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 CMb
{
public partial class Form1 : Form
{
SqlConnection connect = new SqlConnection("Data Source=Admin;Initial Catalog=Practice_DatabaseSQL;User Id=sa;password=p@ssw0rd");

public Form1()
{
InitializeComponent();
bmbBind();
}

void bmbBind()
{
connect.Open();
SqlCommand cmd = new SqlCommand("SELECT Distinct SoftwareName from dbo.Keys", connect);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
cmbNameS.DataSource = dt;
cmbNameS.DisplayMember = "SoftwareName";
cmbNameS.ValueMember = "SoftwareName";
}
}

private void cmbVersion_SelectedIndexChanged(object sender, EventArgs e)
{
string query = "Select SKeys from dbo.Keys where SoftwareName='" + cmbVersion.SelectedValue + "'";
SqlCommand cmd = new SqlCommand(query, connect);
SqlDataReader data =cmd.ExecuteReader();

if ( data.Read())
{
txtKeys.Text = data.GetSqlValue(0).ToString();
}

}

private void cmbNameS_SelectedIndexChanged(object sender, EventArgs e)
{

string query = "Select VersionNo from dbo.Keys where SoftwareName='" + cmbNameS.SelectedValue + "'";
SqlCommand cmd = new SqlCommand(query, connect);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
cmbVersion.DataSource = dt;
cmbVersion.DisplayMember = "VersionNo";
cmbVersion.ValueMember = "VersionNo";
}
}

}
}



I have the following code changes but not able to get the desired output in the textbox
CHill60 15-May-14 3:51am    
I have updated my solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900