0

enter image description here

I am facing unique problem with my code. This is the code for showing tables in comboBox1 its working fine.

ComboBox is filled with school names and when a user select a school in that ComboBox, The students in that school will be shown in DataGridView Please help.

Functionality of this application:

  1. Creating a table in sql database(schoolName).
  2. Selecting table(select School).
  3. Adding values in the school(student information).

error:

  1. the datagrid in the bottom is not retrieving properly, its showing something else.
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\Login.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

con.Open();

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
            
SqlDataAdapter dbAdapter = new SqlDataAdapter(cmd);

DataTable dtRecords = new DataTable();
dbAdapter.Fill(dtRecords);
dataGridView1.DataSource = dtRecords; //dataGrid
comboBox1.DataSource = dtRecords;
comboBox1.DisplayMember = "TABLE_NAME";

con.Close();
5
  • What is your problem exactly? Can u explain the functionality of your application? Commented Aug 9, 2014 at 13:59
  • In dataGrid the values are not binding, its showing different things..! how to get values in datagrid when i select any school from comboBox? Commented Aug 9, 2014 at 14:04
  • Functionality of this application: 1. Creating a table in sql database(schoolName). 2. Selecting table(select School). 3. Adding values in the school(student information). error: 1. the datagrid in the bottom is not retrieving properly, its showing something else. Commented Aug 9, 2014 at 14:10
  • 1
    ComboBox is filled with school names and when a user select a school in that ComboBox, The students in that school will be shown in DataGridView, is it what you want? Commented Aug 9, 2014 at 14:11
  • yes! thats exactly what i want.. :) Commented Aug 9, 2014 at 14:14

1 Answer 1

1

First of all check this: http://msdn.microsoft.com/en-us/library/ms186224.aspx

The SQL query SELECT * FROM INFORMATION_SCHEMA.TABLES returns information about the tables that exist in database not the rows of a specefic table

If you want to the information of that table you can rich that by writing a new query in SelectedIndexChanged event of your ComboBox, something like that:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\Login.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

        con.Open();

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT * FROM " + comboBox1.Text;

        SqlDataAdapter dbAdapter = new SqlDataAdapter(cmd);

        DataTable dtRecords = new DataTable();
        dbAdapter.Fill(dtRecords);
        dataGridView1.DataSource = dtRecords; //dataGrid

        con.Close();
    }

This code can fix this problem, but actually there is a bigger problem and It's about your data structure.

This kind of code tells me there is a table for every school and that's not a normalized database. Lets clear it by an example: Imagine that your manager want to move some students from their school to another school, in this data structure you have to move a record from a table to another table, first you have to find that record that point to our student and then save it in memory and then create a new record in destination table. In this example:

  • There is a lot of works to do (There is a a lot of code to write)
  • There is a risk that your data will be crupted.
  • What happen if this have to be done in 1000 students??!!

In other hand you can simply create table and named it schools that have two main columns: ID , Name and then create a table and named it students that have three main columns: ID, SchoolID, Name every school have a unique id and every student have its own id related to its own school by SchoolID column.

Sign up to request clarification or add additional context in comments.

2 Comments

yeah. you are correct, first i'll complete this project first. I am now trying to add values in the selected tables. how can i use sqlCommand to add values to selected comboboxValueS?
Are you want to add school names to combo box or insert a new student to specified school in combo box?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.