0

I want to select combobox items and want to show top 3 possibilities on datagridview according to my selections in c# 2010 express. I created database using C#.

    private void add_button_Click(object sender, EventArgs e)
    {          
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Mystudies\C#\SQL Database\MakerSelectionDatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

        // insert Logic
        sqlcon.Open();

        SqlDataAdapter sda = new SqlDataAdapter("select * from makerinfo Where [Equipment Type] = '" + equipmenttype_combobox.Text.Trim() + "' and Plant='" + plant_combobox.Text.Trim() + "'", sqlcon);

        // I could not combine this code to combobox selection.
        // SqlDataAdapter eval = new SqlDataAdapter("select top 3 safety from makerinfo order by Safety desc", sqlcon);

        DataTable dt = new DataTable();
        SqlDataAdapter db = new SqlDataAdapter();
        sda.Fill(dt);

        datagridview.DataSource = dt;

        sqlcon.Close();
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
O.Engineer
  • 23
  • 7
  • 1
    Why not use use `Select top ૩` and `order by Saftey Desc` in your query – Chetan Jan 15 '20 at 23:26
  • Thx for your reply. Do you have any sample link or document ? I do not know how can I run with SQL query combined with interface. – O.Engineer Jan 16 '20 at 00:04
  • 1
    `SqlDataAdapter("select top ૩ safety from makerinfo Where [Equipment Type] = '" + equipmenttype_combobox.Text.Trim() + "' and Plant='" + plant_combobox.Text.Trim() + "' order by Saftey desc", sqlcon);` did you try this? – Chetan Jan 16 '20 at 00:34
  • It is done :) Thanks you very much. Now I will try to get all row information. – O.Engineer Jan 16 '20 at 00:55

0 Answers0