new to .net.... why does my sqlconnection keep dis

Results 1 to 2 of 2

Thread: new to .net.... why does my sqlconnection keep dis

  1. #1
    Join Date
    Dec 1969

    Default new to .net.... why does my sqlconnection keep dis

    I have created an .aspx page in .net and it graciously created the codebehind page for me. I added a sqlconnection to my .aspx page and I didn&#039;t want to set the connection string in the page because I&#039;m used to keeping that in an include file or application variable. So, I did some searching around here and found out how to edit the web.config file from ...<BR><BR><BR><BR>However, when I go into the codebehind page and try to add the connection information from the web.config file like so...<BR><BR> private void InitializeComponent()<BR> { <BR> this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(ConfigurationS ettings.AppSettings("connString") );<BR><BR> }<BR><BR>.. and then I click out of the code view, it removes the sqlconnection object from my .aspx page. Then I go back to the codebehind page and the sqlconnection initialization stuff is gone from the InitializeComponent method. So, my question is, how can I create a connection and not have it disappear on me but at the same time, have it look to a variable for it&#039;s connection string? I hope this made sense. Please ask if it didn&#039;t. Thanks.

  2. #2
    Join Date
    Dec 1969

    Default RE: new to .net.... why does my sqlconnection keep

    I do not think that you can add code to that method (I could be wrong. The code-behind in VS.NET generates this:<BR>/// &#060;summary&#062;<BR> /// Required method for Designer support - do not modify<BR> /// the contents of this method with the code editor.<BR> /// &#060;/summary&#062;<BR> private void InitializeComponent()<BR> { <BR> this.Load += new System.EventHandler(this.Page_Load);<BR> }<BR> #endregion<BR><BR>Notice how it says "Do not modify the contents of this method in the code editor". What I usually do is declare a global variable that will hold the connection string:<BR>string mstrConn = ConfigurationSettings.AppSettings["webConn"];<BR>Then in any method that needs a connection, I will use:<BR>OleDbConnection objConn = new OleDbConnection(mstrConn);<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts