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. display data on textboxes from database using classes [modified]

display data on textboxes from database using classes [modified]

Scheduled Pinned Locked Moved C#
csharpdatabasesysadminsecurity
4 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 am still new on C#.i am trying to display data on the texboxes from database using classes but it doesn't display any data where as is there here is my class. public void SeachCustomer(string CName,string CNum) { CustomerName = CName; Barcode1 = CNum; SqlConnection conn = new SqlConnection(); conn.ConnectionString = "integrated security=SSPI;initial catalog=Phusa;server = za211149;persist security info=False"; conn.Open(); try { SqlCommand cmdSearch = new SqlCommand(); cmdSearch.Connection = conn; cmdSearch.CommandText = "SELECT CustomerName FROM Events WHERE Barcode1 =@Barcode1"; SqlParameter Bar = new SqlParameter(); Bar.ParameterName = "@Barcode1"; Bar.Direction = System.Data.ParameterDirection.Output; Bar.Value = Barcode1; cmdSearch.Parameters.Add(Bar); SqlParameter Nam = new SqlParameter(); Nam.ParameterName="@CustomerName"; Nam.Direction = System.Data.ParameterDirection.Output; Nam.Value =CustomerName; cmdSearch.Parameters.Add(Nam); SqlDataReader dr =cmdSearch.ExecuteReader(); while(dr.Read()==true) { CustomerName = dr["CustomerName"].ToString(); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); } -- modified at 4:02 Wednesday 16th August, 2006

    C M 2 Replies Last reply
    0
    • M Mamphekgo

      Hi. i am still new on C#.i am trying to display data on the texboxes from database using classes but it doesn't display any data where as is there here is my class. public void SeachCustomer(string CName,string CNum) { CustomerName = CName; Barcode1 = CNum; SqlConnection conn = new SqlConnection(); conn.ConnectionString = "integrated security=SSPI;initial catalog=Phusa;server = za211149;persist security info=False"; conn.Open(); try { SqlCommand cmdSearch = new SqlCommand(); cmdSearch.Connection = conn; cmdSearch.CommandText = "SELECT CustomerName FROM Events WHERE Barcode1 =@Barcode1"; SqlParameter Bar = new SqlParameter(); Bar.ParameterName = "@Barcode1"; Bar.Direction = System.Data.ParameterDirection.Output; Bar.Value = Barcode1; cmdSearch.Parameters.Add(Bar); SqlParameter Nam = new SqlParameter(); Nam.ParameterName="@CustomerName"; Nam.Direction = System.Data.ParameterDirection.Output; Nam.Value =CustomerName; cmdSearch.Parameters.Add(Nam); SqlDataReader dr =cmdSearch.ExecuteReader(); while(dr.Read()==true) { CustomerName = dr["CustomerName"].ToString(); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); } -- modified at 4:02 Wednesday 16th August, 2006

      C Offline
      C Offline
      coolestCoder
      wrote on last edited by
      #2

      Hi, First of all you haven't added the 'Bar' parameter.

      Mamphekgo wrote:

      cmdSearch.Parameters.Add(Nam);

      As shown above only the 'Nam' param is added.

      Mamphekgo wrote:

      i am trying to display data on the texboxes from database

      You have not written any code to display the data ? Another problem is that CustomerName will only contain the last row's data , bcoz u r not concatenating the strings..

      Mamphekgo wrote:

      CustomerName = dr["CustomerName"].ToString();

      Try to provide complete code if you have missed something or try to correct the above said errors and check for the desired result.


      "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


      Anant Y. Kulkarni

      1 Reply Last reply
      0
      • M Mamphekgo

        Hi. i am still new on C#.i am trying to display data on the texboxes from database using classes but it doesn't display any data where as is there here is my class. public void SeachCustomer(string CName,string CNum) { CustomerName = CName; Barcode1 = CNum; SqlConnection conn = new SqlConnection(); conn.ConnectionString = "integrated security=SSPI;initial catalog=Phusa;server = za211149;persist security info=False"; conn.Open(); try { SqlCommand cmdSearch = new SqlCommand(); cmdSearch.Connection = conn; cmdSearch.CommandText = "SELECT CustomerName FROM Events WHERE Barcode1 =@Barcode1"; SqlParameter Bar = new SqlParameter(); Bar.ParameterName = "@Barcode1"; Bar.Direction = System.Data.ParameterDirection.Output; Bar.Value = Barcode1; cmdSearch.Parameters.Add(Bar); SqlParameter Nam = new SqlParameter(); Nam.ParameterName="@CustomerName"; Nam.Direction = System.Data.ParameterDirection.Output; Nam.Value =CustomerName; cmdSearch.Parameters.Add(Nam); SqlDataReader dr =cmdSearch.ExecuteReader(); while(dr.Read()==true) { CustomerName = dr["CustomerName"].ToString(); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); } -- modified at 4:02 Wednesday 16th August, 2006

        M Offline
        M Offline
        Mamphekgo
        wrote on last edited by
        #3

        actually my code can see that there is data on the database because when i use messagebox to search if ever there is something ot the database it shows that it found the record.but data cannot display on the texboxes,here i am trying to call my class to display data. SearchCustomers search = new SearchCustomers(); string Barcode1; string CustomerName =""; string Barcode; string ProductName=""; string Price = ""; if(txtBarcode.Text.Length <4) { Barcode1 =txtBarcode.Text; CustomerName = lblCustomerName.Text; search.SeachCustomer(CustomerName,Barcode1); } else { Barcode = txtBarcode.Text; lblProductName.Text = ProductName; lblPrice.Text =Price.ToString(); search.SeachProduct(Barcode,ProductName,Price); }

        C 1 Reply Last reply
        0
        • M Mamphekgo

          actually my code can see that there is data on the database because when i use messagebox to search if ever there is something ot the database it shows that it found the record.but data cannot display on the texboxes,here i am trying to call my class to display data. SearchCustomers search = new SearchCustomers(); string Barcode1; string CustomerName =""; string Barcode; string ProductName=""; string Price = ""; if(txtBarcode.Text.Length <4) { Barcode1 =txtBarcode.Text; CustomerName = lblCustomerName.Text; search.SeachCustomer(CustomerName,Barcode1); } else { Barcode = txtBarcode.Text; lblProductName.Text = ProductName; lblPrice.Text =Price.ToString(); search.SeachProduct(Barcode,ProductName,Price); }

          C Offline
          C Offline
          coolestCoder
          wrote on last edited by
          #4

          Hi, Sorry, I didnt got you still. Since you are using the SearchCustomer function to search the db, but it dosent returns any value ?

          Mamphekgo wrote:

          search.SeachCustomer(CustomerName,Barcode1);

          Mamphekgo wrote:

          when i use messagebox to search

          I didnt understand where you are applying this message box ? Inside SearchCustomer or where you are using the object. Sorry but i can see only one problem that ur SearchCustomer function should return the Customer name which you will then set in any text box or other controls. Hope i am guessing it right ! :)


          "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


          Anant Y. Kulkarni

          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