Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Classes [modified]

Classes [modified]

Scheduled Pinned Locked Moved C#
databasesysadminsecurity
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mamphekgo
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • M Mamphekgo

      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

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      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) and search.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; }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups