简体   繁体   中英

Retrieving data in DataGridView by matching TextBox content

I have 3 tables in database as:

  1. Emp(name,cellnumber,payment,Joining_date)
  2. Addmoney(name,date_of money_taken,Type)
  3. AddDate(name,Leaving_Date)

I want to display only these columns from these 3 table (name, payment, Date_of_money_Taken, Joining_date, Leaving_date), when user enters any name stored in Emp table in a TextBox, I have a problem in the code below. It is throwing this:

Exception({"The multi-part identifier \"name.Text\" could not be bound."})

private void ShowEmp_Load(object sender, EventArgs e)
{
    // create the connection string
    connectionString = GetConnectionString();
    connection = new SqlConnection(connectionString);
    queryString = "select Emp.name,Emp.Payment,Emp.JoiningDate,Addm.Date,AddDate.LeavingDate from Emp,Addm,AddDate where name.Text='" + name + " ' ";// Select * From Emp

    // create an SqlDataAdapter to execute the query
    dAdapter = new SqlDataAdapter(queryString, connection);

    // create a command builder
    cBuilder = new SqlCommandBuilder(dAdapter);

    // create a datatable to hold query results
    dTable = new DataTable();

    // fill DataTable
    dAdapter.Fill(dTable);<-EXCEPTION({"The multi-part identifier \"name.Text\" could not be bound."})

    // the DataGridView
    //DataGridView dataGridView1 = new DataGridView();

    // BindingSource to sync DataTable and DataGridView
    bSource = new BindingSource();

    // set the BindingSource DataSource
    bSource.DataSource = dTable;

    // set the DataGridView DataSource
    dataGridView1.DataSource = bSource;

}

Isn't your query wrong? Try this for the query:

queryString = "
    select 
        Emp.name, Emp.Payment, Emp.JoiningDate, Addm.Date, AddDate.LeavingDate
    from
        Emp, Addm, AddDate where Emp.name = '" + name + "' ";

You are referencing name.Text when it looks like the field name should be Emp.name .

Note, you REALLY need to do this using params. This is wide open for a SQL injection...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM