views:

43

answers:

2

Hi..I am using C#.Net and Sql Server ( Windows Application ). I had created a temporary table. When a button is clicked, temporary table (#tmp_emp_answer) is created. I am having another button called "insert Values" and also 5 textboxes. The values that are entered in the textbox are used and whenever com.ExecuteNonQuery(); line comes, it throws an error message Invalid object name '#tbl_emp_answer'.. Below is the set of code.. Please give me a solution.

Code for insert (in insert value button):

private void btninsertvalues_Click(object sender, EventArgs e)  
{
    username = txtusername.Text;
    examloginid = txtexamloginid.Text;
    question = txtquestion.Text;
    answer = txtanswer.Text;
    useranswer = txtanswer.Text;
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=tempdb;Integrated Security=True;");
    SqlCommand com = new SqlCommand("Insert into #tbl_emp_answer values('"+username+"','"+examloginid+"','"+question+"','"+answer+"','"+useranswer+"')", con);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
}
+1  A: 

you need to declare a temp table before using it. So declare it first and then insert values into it.

eugeneK
how to declare ? please give me the syntax ..thanks..
CREATE TABLE #tableName (col1 int,col2 char(30))More important to understand what Ben Robinson said, temporary table exists thought execution of query only, this means even if you create temp table and insert values to it it will exists in the memory until the query finishes and then vanish so you can't select from it.If you want to use your inserted data in future create regular table and fill it with data
eugeneK
another thing, your sql query is wide open for sql injection attacks, please read about SqlCommand.Parameters before some one will 'subquery' you.
eugeneK
ok....thanks a lot..
+7  A: 

Temp tables only exists in the context of and for the duration of the current connection, your connection only has an insert command you are not creating a temp table as such the temp table you are trying to insert into does not exist. Even if you were creating one as part of the same query that inserted the values, it would be dropped as soon as you closed the connection. If you want a table to persist between connections you need to create a normal table, insert the values, then drop it later when you have finished with it.

Ben Robinson
Actually, I created temp table.A button called "Create" has the function of creating temp table.
Yes but temp tables are automatically dropped when the connection is closed and and even while the connection is active they only exist for that connection. You cannot create a temp table with one connection then access it with another connection. The only caveat is you can create global temporary tables by prefixing your table name with ## instead of #, global temp tables can be accessed by other sessions but they are still dropped when all connections that have referenced them are closed.
Ben Robinson
very much thanks...its working fine..