sansat6699 Posted September 28, 2007 Report Share Posted September 28, 2007 i have some oledb code made in c#(vs 2005) it is for local msaccess file. i want to conevert the code for sql server where connection string placed in web.config file seperately. please help me.here is code private void buildGrid() { string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="; conStr += Server.MapPath("~/common/db/demo.mdb"); System.Data.OleDb.OleDbConnection dbCon = new System.Data.OleDb.OleDbConnection(conStr); dbCon.Open(); string sql = "SELECT * FROM Posts ORDER BY LastPostDate DESC"; System.Data.OleDb.OleDbDataAdapter dbAdapter = new System.Data.OleDb.OleDbDataAdapter(sql, dbCon); DataSet ds = new DataSet(); dbAdapter.Fill(ds); ds.Tables[0].TableName = "Inbox"; Grid1.DataSource = ds; }healp me please Link to comment Share on other sites More sharing options...
aspnetguy Posted September 28, 2007 Report Share Posted September 28, 2007 place your connection string in appSettings <appSettings> <add key="ConnectionString" value="Data Source=(local);Initial Catalog=dbname;Trusted_Connection=yes;"/> </appSettings> then private void buildGrid(){ SqlConnection dbCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); dbCon.Open(); string sql = "SELECT * FROM Posts ORDER BY LastPostDate DESC"; SqlDataAdapter dbAdapter = new SqlDataAdapter(sql, dbCon); DataSet ds = new DataSet(); dbAdapter.Fill(ds); ds.Tables[0].TableName = "Inbox"; Grid1.DataSource = ds;} Link to comment Share on other sites More sharing options...
sansat6699 Posted September 29, 2007 Author Report Share Posted September 29, 2007 place your connection string in appSettings<appSettings> <add key="ConnectionString" value="Data Source=(local);Initial Catalog=dbname;Trusted_Connection=yes;"/> </appSettings> then private void buildGrid(){ SqlConnection dbCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); dbCon.Open(); string sql = "SELECT * FROM Posts ORDER BY LastPostDate DESC"; SqlDataAdapter dbAdapter = new SqlDataAdapter(sql, dbCon); DataSet ds = new DataSet(); dbAdapter.Fill(ds); ds.Tables[0].TableName = "Inbox"; Grid1.DataSource = ds;} thank you man thanks very much i will check it Link to comment Share on other sites More sharing options...
sansat6699 Posted October 1, 2007 Author Report Share Posted October 1, 2007 thank you man thanks very much i will check itcode giving error as "connection string not initialized"at dbcon.open(); Link to comment Share on other sites More sharing options...
aspnetguy Posted October 1, 2007 Report Share Posted October 1, 2007 did you add the connection string to web.config? Link to comment Share on other sites More sharing options...
sansat6699 Posted October 3, 2007 Author Report Share Posted October 3, 2007 did you add the connection string to web.config?yes in <connectionstring></connectionstring> section as i am using .net2.0 Link to comment Share on other sites More sharing options...
aspnetguy Posted October 3, 2007 Report Share Posted October 3, 2007 I put it in appSettings and use the code I showed you earlier. <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <appSettings> <add key="ConnectionString" value="Data Source=(local);Initial Catalog=dbname;Trusted_Connection=yes;"/> </appSettings>......</configuration Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now