Click here to Skip to main content
14,302,209 members
Rate this:
Please Sign up or sign in to vote.
See more:
Temp table I assign the values ​​I read in excel table.If the temp table and student table are different, I want to add different ones to the student table.But I can't add with these operations.How can I do it. The SQL command is giving an error and I can't calculate whether it has an int value.

What I have tried:

using(var different=new SqlCommand("SELECT ID,Name,Surname from ##mytemptable except SELECT ID,Name,Surname from Student)),connection)
SqlDataReader dr =different.ExecuteReader();
int newdifferentvalue=0;

if(dr.Read())
{
     newdifferentvalue=dr.GetInt32(0);
using(var different=new SqlCommand("INSERT INTO ID,Name,Surname from Student (SELECT ID,Name,Surname from ##mytemptable except SELECT ID,Name,Surname from Student)),connection)
MessageBox.Show("Different Information is added");
}

else

{
<pre>MessageBox.Show("Different Information has no value.So not added.");

}
Posted
Updated 28-Jul-19 11:19am
Comments
Afzaal Ahmad Zeeshan 28-Jul-19 17:19pm
   
What is the exception that it is giving?
StudentSC 28-Jul-19 17:35pm
   
using(var different=new SqlCommand("SELECT ID,Name,Surname from ##mytemptable except SELECT ID,Name,Surname from Student)),connection) this line give except.But this line
using(var different=new SqlCommand("INSERT INTO ID,Name,Surname from Student (SELECT ID,Name,Surname from ##mytemptable except SELECT ID,Name,Surname from Student)),connection) not insert.command error.
and this line not give int value=>newdifferentvalue=dr.GetInt32(0); //not convert to int value.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

While a DataReader is open, the Connection is in use exclusively by that DataReader. You cannot execute any commands for the Connection, including creating another DataReader, INSERTing data, UPDATEing rows, or any other operation until the original DataReader is closed.

If you try to do it, you will always get an error.
   
Comments
StudentSC 28-Jul-19 17:37pm
   
Thanks for informing.So how can I do in this case.
Afzaal Ahmad Zeeshan 28-Jul-19 17:47pm
   
By closing the DataReader.
StudentSC 28-Jul-19 18:05pm
   
dr.Close(); that way if I finally turn it off

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100