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