help plz: dynamic procedure

** Please see the code below..

I am using this code to populate data from SQL Server to combobox usng SqlDataReader but I have more than 10 combos to populate and I want to use the same procedure with all combos.. which means I will need to pass the combo object and field data..

can you please help...

private void populate_id_type()

{

try

{

//ensure the box is cleared

cboIDType.Items.Clear();

//Use the SQLServer provider..

//set up the connection and the command...

sql_connection = new SqlConnection("server=localhost;uid=sa;pwd=;database=Sales");

string strSQL = "SELECT FirstName + ' ' + LastName as Name FROM Customer";

sql_command = new SqlCommand(strSQL, sql_connection);

//open the connection, and use the reader to populate the combobox

sql_connection.Open();

sql_reader = sql_command.ExecuteReader();

if (sql_reader != null)

{

while (sql_reader.Read())

{

cboIDType.Items.Add(sql_reader["id_type"]);

}

}

//the finally clause will tidy up for us.

cboIDType.SelectedIndex = 0;

}

catch (Exception oE)

{

MessageBox.Show("Problem Populating Reader Box: [" + oE.ToString() + "]");

}

finally

{

if (sql_reader != null) sql_reader.Close();

if (sql_connection != null)

{

if (sql_connection.State == ConnectionState.Open)

sql_connection.Close();

}

}

}




Answer this question

help plz: dynamic procedure

  • D. Roman

    That select statement will produce two table results. For that purpose DataReader has a method NextResult. So you will have two while loops and one NextResult between:
    while (sql_reader.Read())
    {
       cboIDType.Items.Add(sql_reader.GetString(sql_reader.GetOrdinal("id_type)));
    }
    sql_reader.NextResult();
    while (sql_reader.Read()
    {
       cboMaritalStatus.Items.Add(sql_reader.GetString(sql_reader.GetOrdinal("marital_status)));
    }
    I did predict that those fields are of string type, so if they are not string change access methods.



  • jche

    how do you mean they are in different tables The code supplied is shown to get the details from a table "Customer" which is what it will do :-) If you have different tables then you need to execute the queries on those tables as well or append to the existing query to get all the results in 1 go. Example:

    SELECT * FROM table1 SELECT * FROM table2 etc.... and do the same thing as above when reading the values.



  • Jackot

    but they are in different tables

  • ahmed921983

    So you are wanting to fill the comboboxes with the appropriate column values Why dont you just specify the column to read and add the item to the combobox in the while read()

    while (sql_reader.Read())

    {

    cboIDType.Items.Add(sql_reader["id_type"]);

    cboOtherCombo2.Items.Add(sql_reader["field2"]);

    cboOtherCombo3.Items.Add(sql_reader["field3"]);

    ...

    ...

    }

    }



  • DEEPAK GARG

    yes its probably not finished going through the first time and your trying to access the second table. This is an intersting one and I'm just helping out logically but never done it in practice this way. You could just fill a datatable/dataset using a DataAdapter and execute the queries seperately and fill each new datatable.

    Now, by any chance are these 2 tables related if so - you need to perform a join statement so you can get the values in the correct manner



  • michhes

    i tried the code below but getting this error:

    System.IndexOutOfRangeException: marital_status
    at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
    at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
    at System.Data.SqlClient.SqlDataReader.get_Item(String name)
    at shefa.frmPatientDetails.populate_id_type() in C:\Users\Jassim Rahma\Documents\Visual Studio 2005\Projects\shefa\shefa\patient_details_form.cs:line 50

    string strSQL = "SELECT id_type FROM id_type SELECT marital_status FROM marital_status";

    sql_command = new SqlCommand(strSQL, sql_connection);

    //open the connection, and use the reader to populate the combobox

    sql_connection.Open();

    sql_reader = sql_command.ExecuteReader();

    if (sql_reader != null)

    {

    while (sql_reader.Read())

    {

    cboIDType.Items.Add(sql_reader["id_type"]);

    cboMaritalStatus.Items.Add(sql_reader["marital_status"]);

    }

    }



  • help plz: dynamic procedure