0

I have table called TBL_PRODUCTS

CREATE TABLE TBL_PRODUCTS
(
    Products_ID varchar(50) PRIMARY KEY,
    Products_Name varchar(100) NOT NULL,
    Products_Categorys_ID int,
    Products_Qty DECIMAL(16,0),
    Products_Sales_Price DECIMAL(16,0),
    Products_Cost_Price DECIMAL(16,0)
);

and I have a datagridview on my windows application which has 6 columns:

(Products_ID, Products_Name, Products_Categorys_Name, Products_Qty, Products_Sales_Price, Products_Total)

I want the user when he write on the 1st column (Products_ID) the code of the product it show on the same row the other details on that row like (Products_Name, Products_Categorys_Name, Products_Sales_Price) and so on ...

How I can make something like that?

I have tried this code:

SqlConnection con = new SqlConnection("Data Source=GMCADIOM-PC;Initial Catalog=TEMP;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
string sqlqry = "SELECT [Products_ID] as 'كود المنتج',[Products_Name] as 'اسم المنتج',Products_Categorys_Name as 'اسم الصنف',[Products_Qty] as 'الكميه المتاحه',[Products_Sales_Price] as 'سعر البيع',[Products_Cost_Price] as 'سعر الشراء' FROM TBL_PRODUCTS INNER JOIN TBL_PRODUCTS_CATEGORYS ON TBL_PRODUCTS_CATEGORYS.Products_Categorys_ID=TBL_PRODUCTS.Products_Categorys_ID WHERE [Products_ID] LIKE '"+ dgvBillsList.CurrentRow.Cells[0].Value.ToString()+ "'";
SqlCommand cmd = new SqlCommand(sqlqry, con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

        try
        {
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                        dgvBillsList.CurrentRow.Cells["Products_ID"].Value = dt.Rows[i]["كود المنتج"];
                        dgvBillsList.CurrentRow.Cells["Products_Name"].Value = dt.Rows[i]["اسم المنتج"];
                        dgvBillsList.CurrentRow.Cells["Products_Categorys_Name"].Value = dt.Rows[i]["اسم الصنف"];
                        dgvBillsList.CurrentRow.Cells["Products_Sales_Price"].Value = dt.Rows[i]["سعر البيع"];
                }
            }
            else
            {
                var result = MessageBox.Show("هذا المنتج غير موجود بقاعده البيانات ، هل تريد اضافته ؟؟", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    frmProductsNew frm = new frmProductsNew();
                    frm.StartPosition = FormStartPosition.CenterScreen;
                    frm.ShowDialog();
                }
                else if (result == DialogResult.No)
                {
                    //dgvBillsList.CurrentRow.Cells[0].Value = null;
                    //dgvBillsList.CurrentRow.Cells[0].Selected = true;
                    //dgvBillsList.CurrentCell = dgvBillsList.CurrentRow.Cells[0];
                    //dgvBillsList.CurrentCell.Selected = true;
                    //dgvBillsList.BeginEdit(true);
                    //dgvBillsList.CurrentCell = dgvBillsList.Rows[dgvBillsList.Rows.Count].Cells[0];
                }
            }

            CONF_Calculate.Calculate_DGV_Products(dgvBillsList);
            CONF_Calculate.Calculate_DGV_Total(dgvBillsList, txtBillsAmount);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

but it gives me errors on the query side

Object reference not set to an instance of an object on dgvBillsList.CurrentRow.Cells[0].Value.ToString()

Dale K
  • 25,246
  • 15
  • 42
  • 71
GMCadiom
  • 23
  • 1
  • 7
  • Check that dgvBillsList.CurrentRow is not null. Put a breakpoint on that line of code and inspect the CurrentRow property. Most likely that is null. Also check that .Value is not null and you're not calling .ToString() on a null – Jon Apr 21 '19 at 23:59
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ekad Apr 27 '19 at 05:13

0 Answers0