Connection string in a master page's code-behind file

Last post 10-08-2008 1:27 AM by Hong-Gang Chen - MSFT. 4 replies.

Sort Posts:

  • Connection string in a master page's code-behind file

    10-04-2008, 7:17 PM
    • Member
      38 point Member
    • snowieken
    • Member since 02-03-2005, 3:48 AM
    • Posts 23

    Hello,

    I am new to using master pages and pretty much ASP.NET 2.0 in general, but I try to get by with lots of trial and error. However, I think I have encountered a problem I can't find a solution for on my own.

    I am using a master page with a code-behind file, since most of the content pages don't require separate code. This is how my code-behind file of my master page looks:  

    1    Imports System.Data
    2 Imports System.Data.OleDb
    3 4 Partial Class code
    5 Inherits System.Web.UI.MasterPage
    6 7 Dim strConn As String = _
    8 "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    9 Server.MapPath("data/base.mdb")
    10
    11 Dim cn As New OleDbConnection(strConn)
    12 13 Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
    14 15 Try 16 cn.Open()
    17 Dim cm As New OleDbCommand("SELECT COUNT(*) FROM Bannerfotos", cn)
    18 Dim aantalBanners As String = cm.ExecuteScalar()
    19 aantalBanners = 1 + Int(Rnd() * aantalBanners)
    20 21 banner.BackImageUrl = "img/banner" & aantalBanners & ".jpg" 22 23 Finally 24 cn.Close()
    25 End Try
    26 27 End Sub
    28 29 End Class

    At line 7, where my connection string is declared, I'm getting a "Object reference not set to an instance of an object." error message when I try to load my content page. When I put this code in a separate .aspx code-behind file (and I change line 5 to "Inherits System.Web.UI.Page" of course), it works like a charm.

    Does anyone know what I am missing?
     

  • Re: Connection string in a master page's code-behind file

    10-04-2008, 10:59 PM
    • Member
      307 point Member
    • devinmccloud
    • Member since 11-15-2006, 8:54 PM
    • Fargo,ND
    • Posts 104

     I am not very familiar at vb.net but hear is what I do in c#. Use the configuration manager class(Import System.Configuration). Replace "OleDbConnectionString" with your connection string name in the web.config. You should have your connection string set up in the web.config for this to work.

                  
    protected void Page_Load(object sender, EventArgs e)
    {
    if (Page.IsPostBack == false)
    {
    // create the connection
    OleDbConnection myConnection = new OleDbConnection();

    try
    {
    // configure the connection
    string strConnectionString = ConfigurationManager.ConnectionStrings["OleDbConnectionString"].ConnectionString;
    myConnection.ConnectionString = strConnectionString;

    // create the command
    string strCommandText = @"
    SELECT ManufacturerID, ManufacturerName
    FROM Manufacturer
    ORDER BY ManufacturerName"
    ;
                     OleDbCommand myCommand = new OleDbCommand(strCommandText, myConnection);

    // open the database connection
    myConnection.Open();
                   // bind to a control and open a reader or what ever
                }
         catch
                 {
                    //exception code here 
                 }
          finally
                 {
                   myConnection.Close(): 
                 } 
     }
     
     
    										                
    										                
                									    
    Jer
  • Re: Connection string in a master page's code-behind file

    10-05-2008, 7:21 AM
    • Member
      38 point Member
    • snowieken
    • Member since 02-03-2005, 3:48 AM
    • Posts 23

    When I set up my connection string in web.config and go from there, it works like a charm. I still don't know the reason why the connection string in the file broke, but the most important thing is that it works now.

     Thanks!

     EDIT: Spoke too soon... Using the physical path to my database in web.config, it works. However, when I include code to use Server.Mappath because I don't know the physical path on the web server, it gives me the same object reference error.

     
    Snippets:

     web.config:

      <appSettings>
    <add key="Conn" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}" />
    <add key="dbfile" value="data/base.mdb"/>
    </appSettings>

     Declaration of the connection string in my code file:
     

    Dim conn As String = ConfigurationManager.AppSettings("Conn")
    Dim file As String = ConfigurationManager.AppSettings("dbfile")

    Dim strConn As String = String.Format(conn, Server.MapPath(file))

    Dim cn As New OleDbConnection(strConn)
      
    It's the same problem I had in the beginning, I think, since it works on a page not linked to the master page. It throws me an object reference error on the line where strConn is declared. The problem should lie with Server.MapPath I think, but I have no idea what it could be...

  • Re: Connection string in a master page's code-behind file

    10-05-2008, 8:55 AM
    • Member
      38 point Member
    • snowieken
    • Member since 02-03-2005, 3:48 AM
    • Posts 23

    Okay, here I am again Embarrassed

    Provided there won't be any other problems when I upload it to my web server, I think I have got it to work now. The solution was to declare strConn inside Page_Load, not before. When I simply move that declaration line inside Page_Load, it works like a charm.
     

  • Re: Connection string in a master page's code-behind file

    10-08-2008, 1:27 AM
    Answer

    Hi snowieken,

    After read all of your posts and based on my experience, I think the problem cause of different folders.

    So make sure put the MastePage and Page in the same folder, or you should change the path of "base.mdb" dynamically.

    Hong-Gang Chen
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (5 items)