Check for table

M

Morris Neuman

Thanks for the solution Allen. The problem is that the CallMaster.mdb
database is owned by another and seperate telephony service and is placed in
a seperate directory. I cannot put the CallMaster.mdb in the web project's
root directory.

As mentioned your solution works for CallMaster Sql. I now need it to also
work for the CallMaster access version.

Is there another workaround to checking the database to see if certain
tables exist?
 
A

Allen Chen [MSFT]

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
 
M

Morris Neuman

Hi Allen,

As usual, your suggestion helped solve the problem.

We really appreciate your thorough, deatiled and quick response.
--
Thanks again for all your help.

Morris


Allen Chen said:
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
 
A

Allen Chen [MSFT]

Glad to know you've solved this issue, Morris. Thank you for using our
Newsgroup Support Service!

Regards,
Allen Chen
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,075
Messages
2,570,547
Members
47,197
Latest member
NDTShavonn

Latest Threads

Top