Question Displaying data in dataGridView

andrewmanuja

Well-known member
Joined
May 30, 2019
Messages
75
Programming Experience
Beginner
Hi All,
I am trying to get my head around how the 3-layer architecture works using a sample example.
I am able to get the data on to the Presentation Layer, Form_Load event.
Please refer my below code,
C#:
  ClassBusinessLayer objLogic;

        private void Product_Details_Load(object sender, EventArgs e)
        {
            objLogic = new ClassBusinessLayer();
            dataGridViewProducts.DataSource = objLogic.LoadProduct();          
        }
ClassBusinessLayer objLogic is created to access the data from the Business Layer through an object.
When I add a breakpoint at,
C#:
dataGridViewProducts.DataSource = objLogic.LoadProduct();
I am getting the results as follows,

1583898276089.png


However, when I run the code, I am not getting data populated in to the dataGridView named " DataGridViewProducts".
Appreciate your valuable feedback please.

Thank you.
Kind regards,
Andrew
 
Last edited:
You say that something happens when you add a breakpoint but doesn't when you run the code. That seems to suggest that you think that you're not running the code when a breakpoint is hit. That's not the case. The only way you can get code to do anything is to run it. The question is whether you are running it through the debugger or directly and, if in the debugger, whether you are using a breakpoint or not. So, are you saying that you can run the code in the debugger with a breakpoint and see the expected data but without the breakpoint you don;t see the data? Are you saying that running the code in the debugger works as expected regardless of breakpoints but not when running a Release build? Something else?

Regardless, in situations like this, there is plenty that you can do for yourself before posting a question here, which means you can provide much more information if you still need to post here. You can add logging code to tell you where execution is up to and what data is in use if you can't watch with the debugger. You can check connection strings to make sure they are valid in all cases. Etc.
 
Hi jmcilhinney,
What I am saying is, when I am running the code in the debugger mode adding a breakpoint, when I run the respective command line,
C#:
dataGridViewProducts.DataSource = objLogic.LoadProduct();
and check the results just after executing the above command line, it shows the expected results.
However, if I am not in the debugger mode (no breakpoints added), the program executes without any error but doesn't populate the data onto the dataGridView.

1583900941285.png


Hope the question is clear. The connection string works fine.

Appreciate your feedback.
Regards,
Andrew
 
Hi jmcilhinney ,
Please find the attached project code herewith.
The code related to the Data_Layer as follows;
Note
I have created the connection string in the AppConfig file and calling it in the Data_Layer.
C#:
 public class Connection
    {
        private string conn = ConfigurationManager.ConnectionStrings["InventoryConn"].ToString();

        public void InsertUpdateDeleteSQLString(string sqlstring)
        {
            SqlConnection objsqlconn = new SqlConnection(conn);
            objsqlconn.Open();
            SqlCommand objcmd = new SqlCommand(sqlstring, objsqlconn);
            objcmd.ExecuteNonQuery();
        }

        public object ExecuteSqlString(string sqlstring)
        {
            SqlConnection objsqlconn = new SqlConnection(conn);
            objsqlconn.Open();
            DataSet ds = new DataSet();
            SqlCommand objcmd = new SqlCommand(sqlstring, objsqlconn);
            SqlDataAdapter objAdp = new SqlDataAdapter(objcmd);
            objAdp.Fill(ds);
            return ds;
        }

        public void AddNewProductDB(string Product_ID, string Product_Name, int Product_Sales_Price)
        {
            DataSet ds = new DataSet();
            string sql = "INSERT into tbl_Product (Prod_ID,Prod_Name,Sales_Price) VALUES ('"+ Product_ID + "', '"+ Product_Name + "', '"+ Product_Sales_Price + "')";
           
            InsertUpdateDeleteSQLString(sql);
        }
     
        public object LoadProductDB()
        {
            DataSet ds = new DataSet();
            string sql = "SELECT * from tbl_Product order by Prod_ID";
            ds = (DataSet)ExecuteSqlString(sql);
            return ds;
        }
    }

The code of the Business_Layer as follows;
C#:
 public class ClassBusinessLayer
    {      
        public Connection objDataLayer = new Connection();
       
        public void AddNewProduct(string Product_ID, string Product_Name, int Product_Sales_Price)
        {
            objDataLayer.AddNewProductDB(Product_ID, Product_Name, Product_Sales_Price);
        }    

        public object LoadProduct()
        {
            return objDataLayer.LoadProductDB();
        }
    }

Finally, the Presentation_Layer code as follows;
C#:
ClassBusinessLayer objLogic;

        private void Product_Details_Load(object sender, EventArgs e)
        {
            dataGridViewProducts.Rows.Clear();
            objLogic = new ClassBusinessLayer();
            dataGridViewProducts.DataSource = objLogic.LoadProduct();
        }
Additionally, I am calling the Data_Layer as a library in the Business_Layer and Business_Layer as a library in the Presentation_Layer.

Am I missing something with the Value Objects where the values are passed between the Business_Layer and the Presentation_Layer using "get" and "set" methods?

Look forward to hearing from you the mistake I may be doing here.

Thank you.

Kind regards,

Andrew
 
Last edited:
First things first :

C#:
string sql = "INSERT into tbl_Product (Prod_ID,Prod_Name,Sales_Price) VALUES ('"+ Product_ID + "', '"+ Product_Name + "', '"+ Product_Sales_Price + "')";
Get rid of these concatenated strings and introduce parameters, otherwise you leave yourself open to SQL injection attacks. Once you fix that, we can look at the rest of what you've got going on.
 
Back
Top Bottom