locked
(~) or (..) not pointing to virtual path/root , instead it's pointing to system path/root for oledbdataconnection C# asp.net RRS feed

  • Question

  • User-1399280593 posted
    I'm designing a website which works great on my local machine with full path in the connection string as mentioned below <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>private static string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + <o:p></o:p>" C:\\myweb\\Projects\\App_Data\\ASPNetDB.mdb";<o:p></o:p>private static OleDbConnection oledbconn = new OleDbConnection(connectionString);<o:p></o:p>(1)  now i need to host this site on 3rd party server  where i need to write virtual path of  .mdb file , so i tried using (~) as mentioned below<o:p></o:p>private static string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + " ~\\App_Data\\ASPNetDB.mdb";<o:p></o:p>private static OleDbConnection oledbconn = new OleDbConnection(connectionString);<o:p></o:p> <o:p></o:p>which gives me an error <o:p></o:p>'C:\WINDOWS\system32\~\App_Data\ASPNetDB.mdb' is not a valid path.<o:p></o:p> <o:p></o:p>(2)  Then i tried replacing (~) with (..) like mentioned below <o:p></o:p>private static string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + " ..\\App_Data\\ASPNetDB.mdb";<o:p></o:p>private static OleDbConnection oledbconn = new OleDbConnection(connectionString);<o:p></o:p>which gives me an error <o:p></o:p>

    'C:\WINDOWS\App_Data\ASPNetDB.mdb' is not a valid path.

    (3) on the same page if i use AccessDataSource  with below mentioned (Starting with  .. or  ~) for .mdb it resolves virtual path and works great on the 3rd party server as well

    private static AccessDataSource myconn = new AccessDataSource("..\\App_Data\\ASPNetDB.mdb", " Select * from tbltest");

     

     

    I do not know why but it's not poinging  the virtual directory with oledbdataconnection & it is  pointing to my system root i.e c:\windows.... 

    any help will be appreciated a lot ,,,

    i'm writing this code in the code (.cs) file in app_code directory

    with below mentioned references

    using System;<o:p></o:p>using System.Data;<o:p></o:p>using System.Data.OleDb;<o:p></o:p>using System.Configuration;<o:p></o:p>using System.Linq;<o:p></o:p>using System.Web;<o:p></o:p>using System.Web.Security;<o:p></o:p>using System.Web.UI;<o:p></o:p>using System.Web.UI.HtmlControls;<o:p></o:p>using System.Web.UI.WebControls;<o:p></o:p>using System.Web.UI.WebControls.WebParts;<o:p></o:p>using System.Xml.Linq;<o:p></o:p>
    Saturday, December 20, 2008 11:33 PM

Answers

  • User-1319812544 posted

    use this:

    Server.MapPath("~\\App_Data\\ASPNetDB.mdb")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 21, 2008 12:48 AM
  • User-1199946673 posted

    You shouldnot create an AccessDataSourceControl in Code, use normal ADO.NET instead!

    http://forums.asp.net/t/1361023.aspx#2811886

     In web.config. declare the Connection string as follows: 

        <connectionStrings>
        <add name="ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\ASPNetDB.mdb;Persist Security Info=True" providerName="System.Data.OleDb" />
        </connectionStrings>
    
     

     

    In Code, do something like:

    Using (conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString))
    {
      Using (cmd As New OleDbCommand("SELECT * FROM a_Table", conn))
      {
        cmd.CommandType = CommandType.Text;
        conn.Open();
        \\Do Something
      }
    }
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 21, 2008 9:14 AM
  • User-1399280593 posted

    Thanks Very very very much to both of you ,,,,,,,,,,,  i asked the question  to use it in .cs  but my ultimate goal was to use in in web.config both of you answered exectily what i wanted. Realy apprciate the help, and i'm glad that i joined this forum ...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 21, 2008 10:09 AM

All replies

  • User-1319812544 posted

    use this:

    Server.MapPath("~\\App_Data\\ASPNetDB.mdb")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 21, 2008 12:48 AM
  • User-1199946673 posted

    You shouldnot create an AccessDataSourceControl in Code, use normal ADO.NET instead!

    http://forums.asp.net/t/1361023.aspx#2811886

     In web.config. declare the Connection string as follows: 

        <connectionStrings>
        <add name="ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\ASPNetDB.mdb;Persist Security Info=True" providerName="System.Data.OleDb" />
        </connectionStrings>
    
     

     

    In Code, do something like:

    Using (conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString))
    {
      Using (cmd As New OleDbCommand("SELECT * FROM a_Table", conn))
      {
        cmd.CommandType = CommandType.Text;
        conn.Open();
        \\Do Something
      }
    }
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 21, 2008 9:14 AM
  • User-1399280593 posted

    Thanks Very very very much to both of you ,,,,,,,,,,,  i asked the question  to use it in .cs  but my ultimate goal was to use in in web.config both of you answered exectily what i wanted. Realy apprciate the help, and i'm glad that i joined this forum ...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 21, 2008 10:09 AM
  • User-1199946673 posted

    You're welcome! When you're question is answered. please mark the answer(s) that helped you most as the answer?

    http://www.asp.net/community/recognition/

    And I would recommend you to read some articles realating Access, which will answer many questions that might come up when using Acces with ASP.NET:

    http://www.mikesdotnetting.com/Category.aspx?Category=10

    Sunday, December 21, 2008 10:46 AM
  • User-1231836060 posted

    Hi thanks,

            That answer is working well.I am searching this key word for last 2 days thank you very much....Smile


    Thanks & regards,

    Karthick

    Thursday, July 8, 2010 10:15 PM