Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam having a problem in my project I have two web pages(Firstpage.aspx,second.aspx). In Firstpage.aspx i need to check whether the table exist in the database by passing the table name through textbox .If the table name exist it should give an alert msg that table already exist if not it should go to second.aspx.I have written the code using <list string=""> i can able to show whether the table is exist or not but when iam trying to redirect to another page the code i have written for table existing was not working...
Here is my Code......`

enter code here
protected void Page_Load(object sender, EventArgs e)
        {

        }

 public Array GetTableName()
        {
            List<string> result = new List<string>();
            SqlCommand cmd = new SqlCommand("SELECT name FROM sys.Tables", con);
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
                result.Add(reader["name"].ToString());
            return result.ToArray();
        }
 protected void Button3_Click(object sender, EventArgs e)
        {
            var tablenames = GetTableName();
            string Txttblname = txtdatabasetable.Text;

            foreach (var tablename in tablenames)
            {
                if (tablename.ToString() == Txttblname)
                {
                    label1.Text = "This Table Name Already Exist ";

                }
                else if (tablename.ToString() != Txttblname)
                {

                   Session["view1"] = txtdatabasetable.Text;

                   Response.Redirect("CreateTablePage.aspx?value=3");
                }
            }
        }
Posted
Updated 4-Jun-12 20:47pm
v2
Comments
Sandeep Mewara 5-Jun-12 2:52am    
You mean page is not getting redirected ? No error?
Deepthi.B 5-Jun-12 2:57am    
yes my page is getting redirect to second page but it doesn't showing any msg even my table is already existed in my database.

It's a little obvious: Your first time round the the loop or your second will always go to Response .Redirect - because the first time you find a table that isn't called what you are looking for, it will redirect. So if the first table name matches, then you will set the label text, then continue round the loop. On the second time round the loop, the table name will not match (because it did the first time) so it will Redirect.
If the first one doesn't match, then it will redirect immediately.

You need to change it so that you set a variable to "doesn't match" and set it to "match" when you find the table name in teh array. You tehn check the variable after the loop and decide what to do.

In addition:
1) You don't need to check
C#
if (condition)
   ...
else if (!condition)
   ...
Because the else already covers the !condition part.
2) You shouldn't use Array, or var so liberally - use List<T> or IEnummerable<T> instead. It will make you code much more readable.
3) You don't need to do that at all! Just include a WHERE clause in your SELECT statement to return only tables with the name you are looking for...
 
Share this answer
 
call this method:
I write it rght now only for you!:)
C#
public bool IntializingServer(string serverTable, string serverConnection )
        {
            var serverconn = new SqlConnection(serverConnection);
            serverconn.Open();
            var cmdtable = new SqlCommand("if EXISTS (select * from INFORMATION_SCHEMA.tables where table_name = 'Orders') select 'true'else select 'false'",serverconn);
            var dr = cmdtable.ExecuteReader();
                if(dr.GetString(4) == "true")
                {
                    return true;
                }
}
 
Share this answer
 
Comments
Oleksandr Kulchytskyi 5-Jun-12 4:44am    
Graceful implementation, my 5 to U!)
taha bahraminezhad Jooneghani 5-Jun-12 5:03am    
thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900