How can I pass a value from the row of a datagrid view on one windows form to ...

jackie11

New member
Joined
Mar 27, 2015
Messages
3
Programming Experience
1-3
Hi there

I am trying to pass the id value of the selected row in a datagridview in one windows form to another windows form.
How is this done?

I have created a string and put the value of a certain column of the currentRow of the datagridview,
then parsed this string into an int, i know these two bits are working as ive put in Console.WriteLine s to check and the numbers outputted are correct,
I am then passing in this int value to the new object of the form I am trying to send it to, (I have now checked this using another Console.WriteLine and the number is successfully passed through),
then I am using this int value in the new form by passing it to a method in the database class, and asked this method to return a List<Product> object with all the details of the selected row of the datagridview of the first form,
I have declared a new List<Product> in the new form class and have assigned what the method in the database class returns
I have then done another Console.WriteLine to output this new List<Product>, but it doesnt output anything after the database method call(of which should fill it with stuff)

Two reasons I can think of are;

My SQL query in the database method is incorrect, or
you cant use Console.WriteLine to display the stuff in the List<Product> object

Part of our database method including the sql query is this;

C#:
public static List<Product> LoadProduct(int id)
        {
            List<Product> product = new List<Product>();
            OleDbConnection myConnection = GetConnection();
            string myQuery = String.Format("SELECT Product_ID, Product_Name, Product_Description, Price FROM Product where Product_ID = {0}", id);
            OleDbCommand command = new OleDbCommand(myQuery, myConnection);
Can anyone shed any light on this?

Many thanks
Jackie
 

jackie11

New member
Joined
Mar 27, 2015
Messages
3
Programming Experience
1-3
Follow the Blog link in my signature below and check out my three-part post on Data Among Multiple Forms.
Hi there

Sorry I think my thread title was misleading, ive actually managed to successfully pass in the data to the other form, i just want to then use this value in a database query from the second form
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,136
Location
Sydney, Australia
Programming Experience
10+
Hi there

Sorry I think my thread title was misleading, ive actually managed to successfully pass in the data to the other form, i just want to then use this value in a database query from the second form
How would you usually use a value in a database query? Where the value comes from is irrelevant to how it's used. If you don't know how to insert value into a SQL query then check out my blog post on Parameters In ADO.NET.
 
Top Bottom