Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have 6 comboboxes and a button. I also have two tables in my sql database.
First of all i have displayed all the tables in cb1 using:
SQL
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME ASC

The Columns of these two tables are exactly the same BUT they do have different records.
Now i wanna populate the remaining 5 comboboxes(cb2,cb3,cb4,cb5 & cb6) each with a column from the tables in the database
(PrdtName, PrdtCat, Origin, Price, Transtype, Continent)
Afterwards i wanna choose a tbl (dynamic)in cb1 and then selects the column names from the other comboboxes and then display its records on button click.
Here my code so far: 1. stored procedure (this also changes depending on the column name)
SQL
 SELECT DISTINCT [PrdtName] FROM ' + @TableName + ' WHERE [Continent] = ''AF''' +
' ORDER BY [PrdtName] ASC


2. my c# code. This code repeats 5 times depending on the combobox or the column name

C#
 private void FillcbPrdtName()
{

    string S = ConnectionString;
    SqlConnection cn = new SqlConnection(S);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = cn;
    cmd.CommandText = ("[dbo].[spPrdtName]");
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Table_Name", cb2.SelectedValue.ToString());

    if (cbTbl.SelectedValue != null)
    {
        try
        {
            // Open connection

            cn.Open();
            //Read Data only from DB into the cb
            SqlDataReader name= cmd.ExecuteReader();
            while (name.Read())
            {
                cb2.Items.Add(name["PrdtName"]);
            }
            cb2.SelectedIndex = -1;

        }


        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            cn.Close();
        }
    }


There is no ERROR but the 5 comboboxes aren' getting any columns from the database.

Help!!!
Posted
Updated 12-Nov-14 22:28pm
v4
Comments
Dusara Maulik 12-Nov-14 8:36am    
In FillcbPrdtName() function, you are taking selection value of cbTopprojekt drop down and also adding items to the same drop down "cbTopprojekt"?
mikybrain1 12-Nov-14 8:43am    
updated my question. was a mistake
[no name] 12-Nov-14 9:57am    
Why would you tell the user what your tables names are or allow them to select the table names at all?

You should know what the table names are and what data is in them, and let the user select an entry based on your where clause. Why are you doing it this way?
mikybrain1 12-Nov-14 12:25pm    
yeah i understand u but i need help now about this problem if u can help
[no name] 12-Nov-14 15:13pm    
private void Button5_Click(object sender, EventArgs e)
{
SqlConnection sqlConn = default(SqlConnection);
//Creates a SQL Connection should one be called
string SqlConnectionString = "Your connection string here.";
string Query = null;
sqlConn = new SqlConnection();
sqlConn.ConnectionString = SqlConnectionString;
Query = "SELECT `id` FROM `your_db_name`.`your_table` WHERE `username` = @username AND `password` = @password;";

//Providing the username and password are found in the database,
//It will return an Id number if your table has an Id field.
//The QueryComCheck is combining the query and connection as a command.
SqlCommand QueryComCheck = new SqlCommand(Query, sqlConn);
//Define a variable to return the Id to:
int SelectID = 0;
//Define the username and password:
string UsersUsername = "Bob";
string UsersPass = "TestPass";
//The below shows how parameters can be used as placeholders when
//Executing a query statement.
QueryComCheck.Parameters.AddWithValue("@username", UsersUsername);
QueryComCheck.Parameters.AddWithValue("@password", UsersPass);
//The below statement will check your connection state, and
//open it accordingly if needed.
if (sqlConn.State == ConnectionState.Closed) {
sqlConn.Open();
}
//Here we make use of a reader to read and execute our Query command.
using (SqlDataReader reader = QueryComCheck.ExecuteReader) {
//Has Rows will return False if there is nothing to read.
if (reader.HasRows) {
//If there is something to read, the reader will continue reading.
//If there is nothing to read, the reader returns False by default.
while (reader.Read()) {
//Here you can check if the Id has been found based on our Where clause
//in our query string. If the username and password are a match and the
//Id exists, then you can receive the result here.
if (Convert.ToBoolean(reader.GetString("id")) == true) {
//Now set it to a variable or do what you want with that result.
SelectID = Convert.ToInt32(reader.GetString("id"));
//If you want additional checks for other values, add an ElseIf extension
//to this section of the If Statement.
}
}
}
}
//All done, if there are any mistakes, or errors, you will need to debug
//them yourself, as this was originally for MySQL, and I just changed everything to
//SQL. But in a nutshell, that's more or less how you would do it.
//If you 'have any questions, I can try to answer them for you. Hope it helps.

}

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