Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Separating repeating code in c#

Status
Not open for further replies.

mellenburg

Programmer
Aug 27, 2001
77
0
0
US
I'm very new to programming in .Net and object-oriented programming. I have a working program that can be refactored by putting repeated lines of code in a single location and the n referencing that piece of code. For example, I have the following code in my OnLoad event:

Code:
private void Page_Load(object sender, System.EventArgs e)
{
  if (!IsPostBack) 
  {
    SqlConnection sqlConn = new SqlConnection("Data Source=XXXX;Database=XXXX;user id=XXXX;password=XXXX;");
    
    // Retrieve User appointments
    SqlCommand sqlGetAppointments = new SqlCommand("MassageGetAppointmentsForUser", sqlConn);
    sqlGetAppointments.CommandType = CommandType.StoredProcedure;
    sqlGetAppointments.Parameters.Add("@UserID", SqlDbType.VarChar).Value = User.Identity.Name;
    sqlConn.Open();
    SqlDataReader sqlDRAppointments = sqlGetAppointments.ExecuteReader();
    dgMassageAppointments.DataSource=sqlDRAppointments;
    dgMassageAppointments.DataBind();
    sqlConn.Close();  
  }
}
The SqlConnection line gets used in other places in the code behind file. Rather than have the entire connection string, I'd like to be able to replace the line with something like

GetConnection();

How do I do this?

Matt
 
I would put that code in a function in a class file that returns a string.
 
use the connection string settings in the web.config to store the connection string. you can encrypt the connection string in the web.config if security is an issue.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I'm very new to c# and OOP, so I understand both suggestions, but I don't know how to do either. Can someone give me some sample code?
 
Ok, so I have this in my web.config page now.

Code:
 <appSettings>
		<add key="SqlConnect" value="server=xxxx;database=xxx;uid=xxx;pwd=xxx;" />		
 </appSettings>

How do I make use of the SqlConnect key instead of writing the line

Code:
    SqlConnection sqlConn = new SqlConnection("Data Source=XXXX;Database=XXXX;user id=XXXX;password=XXXX;");

in my onload event?
 
If you've placed the variable in the appSettings section then you would use:
Code:
ConfigurationManager.AppSettings["MyKey"]
However, connection strings should be placed in the connectionStrings section and retrived by using:
Code:
ConfigurationManager.ConnectionStrings["myConKey"].ConnectionString
You may also want to read

____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top