Hi Morris,
If the mdb file cannot be moved I suggest you use SqlDataSource instead of
AccessDataSource to connect to the Access database. You can refer to the
following documentation to learn how to do so.
http://msdn.microsoft.com/en-us/library/ms247233(VS.80).aspx
The exception is not caused by the code to check the existing tables in
Access database, but by the invalid path specified for the AccessDataSource
control. When SqlDataSource is used we can use the connectionstring instead
of the virtual path to specify the location of the database file.
After the exception is eliminated we can still use the code I provided
before to check the existing of the table in Access database. I'd like to
repaste the code here.
Aspx:
<asp:TextBox ID="TextBox1" runat="server" Text="Orders"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:Button ID="Button1"
runat="server" Text="Check" onclick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server">Yes the table
is in the database!</asp:LinkButton>
</ItemTemplate></asp:TemplateField></Columns>
</asp:GridView>
Aspx.cs:
protected void Button1_Click(object sender, EventArgs e)
{
using (OleDbConnection oc = new
OleDbConnection(ConfigurationManager.ConnectionStrings["Database1ConnectionS
tring"].ToString()))
{
oc.Open();
DataTable dt =oc.GetSchema("tables");
//The following code just shows the table on the page,
which can provide a direct vision of the data retrieved.
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
///////////////////////////
//The key here, to see if the table exists in the database
if you also want to check the views, please check dr[0] only.
string tablename = this.TextBox1.Text;
bool hasfound = false;
foreach (DataRow dr in dt.Rows)
{
if (dr[3].ToString() == "TABLE" &&
dr[2].ToString() == tablename)
{
Label1.Text = "The table " + tablename + " is in
the database";
this.GridView1.Columns[0].Visible = true;
hasfound = true;
break;
}
}
if (!hasfound)
{
Label1.Text = "The table " + tablename + " is NOT in
the database";
this.GridView1.Columns[0].Visible = false;
}
}
}
web.config:
...
<add name="Database1ConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program
Files\CallMaster\Data\Callmaster.mdb" providerName="System.Data.OleDb"/>
...
You can see no AccessDataSource control is used here so it's not related to
the exception "....is not a valid virtual path".
Regards,
Allen Chen
Microsoft Online Support