简体   繁体   中英

How to display invoice items from one DataGridView (Invoice) to another DataGridView (Items) c#

I'm using VS and SQL Server. I have problem with query:

SELECT * FROM dbo.ul_dok WHERE (dok_id_fk = @dok_id_fk)

When I run query in SQL Server, everything is working great, window pops to ask me for dok_id_fk, and I insert it manually.

I have entered this code in c#:

conn.Open();

        cmd = new SqlCommand("select ul_dok_id, ul_dok_dat, dok_id_fk, fil_model_no_fk, kol_ul_dok " +
            " from ul_dok where dok_id_fk = @dok_id_fk", conn);
        cmd.Parameters.AddWithValue("@dok_id_fk", txt_Dokument.Text);
        cmd.ExecuteNonQuery();
        grb_Stavke_Dok.Show();

 conn.Close();

Everything else is working, I just can't show product items of the invoice document.

Have set on Invoice DataGridView that when I click on cell to load ID of the invoice to TextBox (txt_Dokument), and then re-use it in the query that is listing products with the same Invoice ID.

If this isn't enough info, pls tell me to provide more. At the moment I cant be more specific about my problem.

You are performing a SELECT operation and calling ExecuteNonQuery() ... that's wrong cause that will return you only the number of rows affected and not the result set you expected. Rather you should call ExecuteReader() like

cmd.ExecuteReader()

This was a solution to my problem, and here is the code.

private void DisplayData_Sa_istom_Sifrom()
    {

        conn.Open();
        DataTable dt_Stavke = new DataTable();
        da = new SqlDataAdapter("select ul_dok_dat as [Datum], fil_model_no_fk as [Oznaka filtera], kol_ul_dok as [Kolicina] " +
            " from ul_dok where dok_id_fk = @dok_id_fk", conn);
        da.SelectCommand.Parameters.AddWithValue("@dok_id_fk", txt_Dokument.Text);
        da.SelectCommand.ExecuteScalar();
        da.Fill(dt_Stavke);
        dgv_stavke.DataSource = dt_Stavke;

        conn.Close();
    }

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