Check for table

Discussion in 'ASP .Net Web Controls' started by Morris Neuman, Jan 5, 2009.

  1. 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?
     
    Morris Neuman, Jan 23, 2009
    #21
    1. Advertisements

  2. 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
     
    Allen Chen [MSFT], Jan 23, 2009
    #22
    1. Advertisements

  3. 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


     
    Morris Neuman, Jan 23, 2009
    #23
  4. Glad to know you've solved this issue, Morris. Thank you for using our
    Newsgroup Support Service!

    Regards,
    Allen Chen
    Microsoft Online Community Support
     
    Allen Chen [MSFT], Jan 27, 2009
    #24
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.