Avatar of LearningCSharpSQL
LearningCSharpSQL

asked on 

Insert code into text box from .cs class

Hi
I do not want to use the listview dataview etc in .NET2.0
Can any one tell me how to insert the following code in a .cs class into a textbox on a aspx page

public CustomerBLL GetCustomerByID(int orderNumber)
            {
            SqlConnection con = new SqlConnection(DBConn);
            SqlCommand cmd = new SqlCommand("ats_SelectCustomerDetails", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int, 4));
            cmd.Parameters["@CustomerID"].Value = orderNumber;

            try
                {
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleRow);
                rdr.Read();
                CustomerBLL cust = new CustomerBLL(
                    (int)rdr["OrderNumber"],
                    (string)rdr["CustomerEmail"],
                    (string)rdr["CustomerPassword"],
                    (string)rdr["CustomerName"],
                    (string)rdr["CustomerPosition"],
                    (string)rdr["CustomerCompany"],
                    (string)rdr["CustomerAddress"],
                    (string)rdr["CustomerCity"],
                    (string)rdr["CustomerCounty"],
                    (string)rdr["CustomerPostCode"],
                    (string)rdr["CustomerCountry"],
                    (string)rdr["CustomerPhone"]);
                rdr.Close();
                return cust;
                }
            catch (SqlException)
                {
                throw new ApplicationException("Sorry an error has occured<br /> and we cannot process your request.<br /> Error code 1001");
                }
            finally
                {
                if (con.State == ConnectionState.Open)
                    {
                    cmd.Dispose();
                    con.Close();
                    con.Dispose();
                    }

textbox text="Need to add code her"
C#

Avatar of undefined
Last Comment
gbzhhu
Avatar of Niraj_Singh
Niraj_Singh
Flag of India image

hi,
 you can move your function GetCustomerByID(int orderNumber) to a class file(.cs)
 now in your aspx's code behind you can create an object of the class file and give a call to GetCustomerByID(int orderNumber) function. since the return type of this function is of type CustomerBLL, you will also need an object of this type to collect he returned value.
 then using this object you can assign the text to your textbox.

Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

Following on Niraj_Singh's suggestion I would override ToString() method in the CustomerBLL class so that it returns something like

Order Number:      111111
Customer Email:     cust@customer.com
and so on...

Then
textbox text = returnedCust.toString();

Am I making sense?

H
 
Avatar of LearningCSharpSQL
LearningCSharpSQL

ASKER

Hi
Following on from your comments, i'm not sure what you mean. In my aspx.cs file i have:
private CustomerDAL CAL = new CustomerDAL();

so in page_load will i have:

TextBox1.Text = CAL.GetCustomerByID <---Not sure what goes here

George
ASKER CERTIFIED SOLUTION
Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of LearningCSharpSQL

ASKER

Hi gbzhhu:

Thanks, a bit of tweaking and it works OK, one question the StringBuilder populates 15 textboxes, is the much of a peformance hit using a stringbuilder.

George
Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,

Remember that a string in .NET is immutable (you cannot change them).  That means that if you create a string
string myStr = "Hello";

Then you

myStr += " World!";

A new string is created and assigned the concatenated value "Hello World!".  The old string myStr is discarded.  This is where performance is hit AND it is recommended that you use a StringBuilder to get better performance

Check this
http://www.codeproject.com/Purgatory/string.asp

C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo