Last post Oct 18, 2009 06:44 AM by hans_v
Oct 17, 2009 05:20 PM|abbanerjee|LINK
I am newbie to ASP.Net.
I want to query database on page_load and assign label.text values using information stored in database. I am using the code below.
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; data source=" + System.Web.HttpContext.Current.Server.MapPath("App_Data/AppointmentManager.mdf");
//string connectionString = "C:\Documents and Settings\xx\My Documents\Visual Studio 2005\WebSites\Proto1\App_Data\xyz.mdb";
Label1.Text = connectionString.ToString(); string selectSQL;
selectSQL = "SELECT FNAME, LNAME, ID FROM MEMBER";
// Define the ADO.NET objects.
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(selectSQL, con);
// Try to open database and read information.
reader = cmd.ExecuteReader();
// For each item, add the author name to the displayed
// list box text, and store the unique ID in the Value property.
ListItem newItem = new ListItem();
//newItem.Text = reader["LastName"] + ", " + reader["FirstName"];
//newItem.Value = reader["ID"].ToString();
Label2.Text = reader["FNAME"] + ", " + reader["LNAME"];
catch (Exception err)
Label3.Text = "Error reading list of names. ";
Label3.Text += err.Message;
When I try this - I get the following error message.
Error reading list of names. Could not use ''; file already in use.
Can someone help me what I need to do to query database on page_load ?
Oct 17, 2009 06:46 PM|abbanerjee|LINK
Okay. I figured it out. Basically, I had the database connection open in the solution explorer.
Now, I am not able to connect because
I read a bit on internet and I dont understand the answers. Does it mean I cannot use a .mdf file to connect. I want to connect to the database, retrieve few values and set properties of text boxes using these values. Any workarrounds ?
Oct 17, 2009 07:11 PM|hans_v|LINK
Does it mean I cannot use a .mdf file to connect
Off course you can, but an .mdf file is a SQL Server (express) file, not Access, so you should not use OleDb
Oct 17, 2009 07:49 PM|abbanerjee|LINK
Can you please help me with a reference on how I should use it ? I am really a newbie to this and any help would be appreciated. The workarround I am using right now is to use a SqlDataSource and then iterate through it. Is there a way to establish the connection
Oct 17, 2009 08:14 PM|hans_v|LINK
string selectSQL = "SELECT FNAME, LNAME, ID FROM MEMBER;"
string connectionString = @"Server=.\SQLExpress;AttachDbFilename=|DataDirectory|AppointmentManager.mdf;Database=AppointmentManager;Trusted_Connection=Yes;";
using (SqlConnection con = new SqlConnection(connect))
using (SqlCommand cmd = new SqlCommand(query, con))
reader = cmd.ExecuteReader();
Oct 17, 2009 09:07 PM|abbanerjee|LINK
I will try this out and post my results. In the meantime I am wrestling with ViewState not popping up the ViewState.Item in Intellisense. Any ideas/clues why that is happening?
Oct 18, 2009 06:44 AM|hans_v|LINK
In the meantime I am wrestling with ViewState not popping up the ViewState.Item in Intellisense. Any ideas/clues why that is happening?
I'm not sure what you mean with this question. But this has nothing to do with the original question, so please opemn another thread for this: