Classes [modified]
-
Hi. I have created a class that read data from the database and i want to display this data on labels. here is my class public void SeachProduct(string PBarcode,string PName,string PPrice) { Barcode = PBarcode; ProductName = PName; Price = PPrice; SqlConnection conn1 = new SqlConnection(); conn1.ConnectionString = "integrated security=SSPI;initial catalog=Phusa;server = za211149;persist security info=False"; conn1.Open(); try { SqlCommand cmdSearch1 = new SqlCommand(); cmdSearch1.Connection = conn1; cmdSearch1.CommandText = "SELECT ProductName,Price FROM Products WHERE Barcode=@Barcode"; SqlParameter Bar = new SqlParameter(); Bar.ParameterName = "@Barcode"; Bar.Direction = System.Data.ParameterDirection.Input; Bar.Value = Barcode; cmdSearch1.Parameters.Add(Bar); SqlParameter Nam1 = new SqlParameter(); Nam1.ParameterName="@ProductName"; Nam1.Direction = System.Data.ParameterDirection.Output; Nam1.Value =ProductName; cmdSearch1.Parameters.Add(Nam1); SqlParameter Pri= new SqlParameter(); Pri.ParameterName = "@Price"; Pri.Direction = System.Data.ParameterDirection.Output; Pri.Value = Price; cmdSearch1.Parameters.Add(Pri); SqlDataReader dr1 =cmdSearch1.ExecuteReader(); if(dr1.Read()==true) { //MessageBox.Show("Product found"); ProductName= dr1["ProductName"].ToString(); Price = dr1["Price"].ToString(); } else { MessageBox.Show("Product not found"); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { conn1.Close(); } and here i am try to call so that it can display data on the labels my class on form SearchCustomers search = new SearchCustomers(); string Barcode; string ProductName=""; string Price = ""; Barcode = txtBarcode.Text; search.SeachProduct(Barcode,ProductName,Price); lblProductName.Text = ProductName; lblPrice.Text =Price; } -- modified at 4:03 Thursday 17th August, 2006
-
Hi. I have created a class that read data from the database and i want to display this data on labels. here is my class public void SeachProduct(string PBarcode,string PName,string PPrice) { Barcode = PBarcode; ProductName = PName; Price = PPrice; SqlConnection conn1 = new SqlConnection(); conn1.ConnectionString = "integrated security=SSPI;initial catalog=Phusa;server = za211149;persist security info=False"; conn1.Open(); try { SqlCommand cmdSearch1 = new SqlCommand(); cmdSearch1.Connection = conn1; cmdSearch1.CommandText = "SELECT ProductName,Price FROM Products WHERE Barcode=@Barcode"; SqlParameter Bar = new SqlParameter(); Bar.ParameterName = "@Barcode"; Bar.Direction = System.Data.ParameterDirection.Input; Bar.Value = Barcode; cmdSearch1.Parameters.Add(Bar); SqlParameter Nam1 = new SqlParameter(); Nam1.ParameterName="@ProductName"; Nam1.Direction = System.Data.ParameterDirection.Output; Nam1.Value =ProductName; cmdSearch1.Parameters.Add(Nam1); SqlParameter Pri= new SqlParameter(); Pri.ParameterName = "@Price"; Pri.Direction = System.Data.ParameterDirection.Output; Pri.Value = Price; cmdSearch1.Parameters.Add(Pri); SqlDataReader dr1 =cmdSearch1.ExecuteReader(); if(dr1.Read()==true) { //MessageBox.Show("Product found"); ProductName= dr1["ProductName"].ToString(); Price = dr1["Price"].ToString(); } else { MessageBox.Show("Product not found"); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { conn1.Close(); } and here i am try to call so that it can display data on the labels my class on form SearchCustomers search = new SearchCustomers(); string Barcode; string ProductName=""; string Price = ""; Barcode = txtBarcode.Text; search.SeachProduct(Barcode,ProductName,Price); lblProductName.Text = ProductName; lblPrice.Text =Price; } -- modified at 4:03 Thursday 17th August, 2006
To make your code work as it is now, you have to use the "ref" keyword on the parameters, so that the method can change the strings that you send into it:
public void SeachProduct(string PBarcode, ref string PName, ref string PPrice)
andsearch.SeachProduct(Barcode, ref ProductName, ref Price);
To use reference parameters in this ways is rarely used, though. Store the values in the search object instead, and expose them through properties.--- b { font-weight: normal; }