views:

26

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_details) 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();
        }
A: 

You said you created the temporary table #tmp_emp_details but are trying to insert into a table of a different name, #tbl_emp_answer.

  • Have you created the table #tbl_emp_answer?
  • Should you be inserting into #tmp_emp_details instead?
lc
A: 

"There are two types of temporary tables: local and global. Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server. Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server"

#tbl_emp_answer - is local temporary table.

##tbl_emp_answer - is global temporary table.

igor