Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Check for table

Reply
Thread Tools

Check for table

 
 
Morris Neuman
Guest
Posts: n/a
 
      01-05-2009
Hi,
How do I check if a table exists in my database?
--
Thanks
Morris
 
Reply With Quote
 
 
 
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      01-06-2009
Hi Morris,

If you're using SQL Server you can try:

SELECT TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES

Check the result you can see all the base tables and views of the database.

Please let me know if it works. If you have further questions please feel
free to ask.

Regards,
Allen Chen
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
 
 
 
Morris Neuman
Guest
Posts: n/a
 
      01-07-2009
Thanks for the reply.

I did not make myself very clear. I want to do the following:

I have a hyperlink field as a column in a gridview. I only want to set this
hyperlink/column to be visible if a table (mailboxactivitylog) exists in
either the sql or access database bound to the gridview via datasource.

How do I check if this table exists? I have a master page and the gridview
is on the content page.
--
Thanks
Morris


"Allen Chen [MSFT]" wrote:

> Hi Morris,
>
> If you're using SQL Server you can try:
>
> SELECT TABLE_NAME, TABLE_TYPE
> FROM INFORMATION_SCHEMA.TABLES
>
> Check the result you can see all the base tables and views of the database.
>
> Please let me know if it works. If you have further questions please feel
> free to ask.
>
> Regards,
> Allen Chen
> Microsoft Online Community Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> .
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#notifications.
>
> Note: MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 2 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions. Issues of this
> nature are best handled working with a dedicated Microsoft Support Engineer
> by contacting Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

 
Reply With Quote
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      01-07-2009
Hi Morris,

Thanks for your clarification. If my understanding is correct you have two
questions:

1. How to know if a tables exists in the database.
2. How to hide a column of a GridView manually.

To the first question, we can query the database to see all the tables in
it. Then check if the table exists in the database. Generally database has
some system tables that can help to do this. For SQL Server, it's
INFORMATION_SCHEMA.TABLES. So we can use the following query:

SELECT TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES

For Access database we use MSysObjects. Here's the query for Access:

Select Name from MSysObjects

We can check if the table name exists in the result of the query and then
decide whether to hide the column of the GridView, which is your second
question.

To hide the column we can try:

void GridView1_PreRender(object sender, EventArgs e)
{
GridView g = (GridView)sender;
g.Columns[0].Visible = false;.//Change the index please. Here 0
is used.
}

If it's not what you need please provide your current code. I think it
would be a great start for us to discuss based on your code.

Regards,
Allen Chen
Microsoft Online Community Support

 
Reply With Quote
 
Morris Neuman
Guest
Posts: n/a
 
      01-08-2009
Hi,

Thanks for the response. I tried your method but could not get it to work.
I checked the MSDN help and tried examples of using the DataTableCollection
through the Tables property, however could not get that to work either.

1) Can you show in the code below how I would use your mehtod as well as the
one using DataTableCollection?
2) Which is the better way to check for a table?

My code is:

protected void Page_Load(object sender, EventArgs e)
{
//check web.config if system app setting set for sql or access
//MyDataSource will always be either SqlDataSource1 or
AccessDataSource1
string id = ConfigurationManager.AppSettings["MyDataSource"];
Control datasourcecontrol = this.FindControl(id);


// Get the DataSet of a DataGrid.
//AS I DON'T HAVE a GRID NOT SURE HOW TO GET THE DATASET
//dSet = (DataSet)DataGrid1.DataSource;

// Get the DataTableCollection through the Tables property.
DataTableCollection tablesCol = dSet.Tables;

// Check if the named table exists.
if (tablesCol.Contains("MailboxActivityLog"))
{
HyperLink11.Visible = "true";
}
else
{
HyperLink11.Visible = "false";
}
}


3) I also found an example using the following in the page load but could
not get that to work either.
if Exists (Select MailboxActivityLog From INFORMATION_SCHEMA.TABLES)
{
HyperLink11.Visible = "true";
}
else
{
HyperLink11.Visible = "false";
}

Once again, I am looking to you for help and to point me in the right
direction.
--
Thanks
Morris


"Allen Chen [MSFT]" wrote:

> Hi Morris,
>
> Thanks for your clarification. If my understanding is correct you have two
> questions:
>
> 1. How to know if a tables exists in the database.
> 2. How to hide a column of a GridView manually.
>
> To the first question, we can query the database to see all the tables in
> it. Then check if the table exists in the database. Generally database has
> some system tables that can help to do this. For SQL Server, it's
> INFORMATION_SCHEMA.TABLES. So we can use the following query:
>
> SELECT TABLE_NAME, TABLE_TYPE
> FROM INFORMATION_SCHEMA.TABLES
>
> For Access database we use MSysObjects. Here's the query for Access:
>
> Select Name from MSysObjects
>
> We can check if the table name exists in the result of the query and then
> decide whether to hide the column of the GridView, which is your second
> question.
>
> To hide the column we can try:
>
> void GridView1_PreRender(object sender, EventArgs e)
> {
> GridView g = (GridView)sender;
> g.Columns[0].Visible = false;.//Change the index please. Here 0
> is used.
> }
>
> If it's not what you need please provide your current code. I think it
> would be a great start for us to discuss based on your code.
>
> Regards,
> Allen Chen
> Microsoft Online Community Support
>
>

 
Reply With Quote
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      01-09-2009
Hi Morris,

Thanks for your update. I've made a sample that demonstrates how to do
this. The database I used is the Northwind database. If you have no that
database you can change the NorthwindConnectionString1 setting in the
web.config:


<connectionStrings>
<add name="NorthwindConnectionString1" connectionString="Your Connection
String" providerName="System.Data.SqlClient"/>
</connectionStrings>


Here's the code.

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 (SqlConnection sc = new
SqlConnection(ConfigurationManager.ConnectionStrin gs["NorthwindConnectionStr
ing1"].ToString()))
{

SqlCommand command = new SqlCommand(@"SELECT
TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES", sc);
SqlDataAdapter sa = new SqlDataAdapter(command);
DataTable dt = new DataTable();
sa.Fill(dt);

//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[1].ToString() == "BASE TABLE" &&
dr[0].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;
}
}
}

To test, you can enter an arbitary table name in the TextBox and click the
"Check" button. You'll see the Lable shows if the table exists in the
database and a hyperlink column in the GridView will also be
visible/invisible according to the result.
As to Access database we don't use:

SELECT TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES

We use this query instead:

Select Name from MSysObjects

If there's anything unclear please feel free to let me know. I'll do my
best to provide a clearer explanation.

Regards,
Allen Chen
Microsoft Online Support

 
Reply With Quote
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      01-13-2009
Hi Morris,

I'm contacting you to see if you have tested my code and what the test
result is. I want to see if the information provided was helpful. Please
keep me posted on your progress and let us know if you have any additional
questions or concerns.

I'm looking forward to your response.


Regards,
Allen Chen
Microsoft Online Community Support

 
Reply With Quote
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      01-13-2009
Hi Morris,

I'm contacting you to see if you have tested my code and what the test
result is. I want to see if the information provided was helpful. Please
keep me posted on your progress and let us know if you have any additional
questions or concerns.

I'm looking forward to your response.


Regards,
Allen Chen
Microsoft Online Community Support

 
Reply With Quote
 
Morris Neuman
Guest
Posts: n/a
 
      01-14-2009
Hi Allen,

Had to make some changes to a section of the website and am running into
some problems, so have not had a chance to try your code. Hopefully once I
can get that resolved then I will get back to try your solution.

A quick follow-up, why doen's the code below work?

if Exists (Select MailboxActivityLog From INFORMATION_SCHEMA.TABLES)
{
HyperLink11.Visible = "true";
}
else
{
HyperLink11.Visible = "false";
}

--
Thanks
Morris


"Allen Chen [MSFT]" wrote:

> Hi Morris,
>
> I'm contacting you to see if you have tested my code and what the test
> result is. I want to see if the information provided was helpful. Please
> keep me posted on your progress and let us know if you have any additional
> questions or concerns.
>
> I'm looking forward to your response.
>
>
> Regards,
> Allen Chen
> Microsoft Online Community Support
>
>

 
Reply With Quote
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      01-14-2009
HI Morris,

Thanks for your update.

Quote from Morris============================================ ======

A quick follow-up, why doen's the code below work?

if Exists (Select MailboxActivityLog From INFORMATION_SCHEMA.TABLES)
{
HyperLink11.Visible = "true";
}
else
{
HyperLink11.Visible = "false";
}
==================================================

Do you get any exception when compiling the above code? If the Exist is a
method written by you that has one string parameter and return bool you can
try:

if(Exists ("SELECT * FROM INFORMATION_SCHEMA.TABLES") )

Another problem is, the MailboxActivityLog is not a column of the returned
table. You can check the returned table of the query:

SELECT *
FROM INFORMATION_SCHEMA.TABLES

The * means to select all the column. So the returned table contains all
the column of the table INFORMATION_SCHEMA.TABLES. You can see
MailboxActivityLog is not there. Could you let me know what this column is?

Regards,
Allen Chen
Microsoft Online Support

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to selecet check box in the data grid ?? only one check box mit ASP .Net 1 01-25-2006 06:47 PM
how to check all check boxes in repeater control kris ASP .Net 0 09-22-2005 06:29 PM
Table/table rows/table data tag question? Rio HTML 4 11-05-2004 08:11 AM
.Net Pro include Visual Source Safe or other Code Check in Check out software? Davisro ASP .Net 1 06-14-2004 03:23 PM
Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table. David Williams ASP .Net 2 08-12-2003 07:55 AM



Advertisments