Click here to Skip to main content
15,895,774 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi dears,

I have created one datatble called temp.

and it is having some values, when i give temp as datasource for grid view, then the value is populating. But when i copy the same to my database table using SQLbulkcopy, I am getting the below error in line--

sbc.writetoserver(temp);

"object reference not set to an instance object"

Actually its popping up as an exception.

What I have tried:

datatable temp is having values.
double checked "con" is my sql connectionstings name.
home.global.mon ----> table name taking from a global variable.



C#
using(sqlbulkcpoy sbc =new sqlbulkcop(con))
{
sbc.destinationtable=home.global.mon;
 sbc.writetoserver(temp); // Getting error here 
con.close();
}
Posted
Updated 27-Jul-18 12:16pm
v3
Comments
RossMW 26-Jul-18 20:37pm    
I think you will need to supply more of your code then just where the error appears. Constructs, database setting and connection opening etc.
dan!sh 27-Jul-18 2:16am    
Is the column mapping correct? Are you provide data for all not null fields?
CHill60 27-Jul-18 3:34am    
What is a sqlbulkcop? What is temp and has it been initialised? What is con and has it been intialised? One of them has not been initialised - debug to determine which one
Member 13858616 27-Jul-18 15:29pm    
temp is having values and con is my connection string. Everything is perfect still error.
Member 13858616 27-Jul-18 16:24pm    
How the temp can be initialized?? Its already has some table values and when i bind it to a grid, i can see the values.

1 solution

Make sure there is space between the = sign and new ( ... = new SqlBulkCopy(con)
Make sure that con has been instantiated AND opened. Then try again.
 
Share this answer
 
Comments
Member 13858616 27-Jul-18 19:17pm    
Yea i put a space between = and new. Also con is initialized just befor the SQLBulkCopy
Member 13858616 27-Jul-18 19:18pm    
Still it shows error.
Member 13858616 30-Jul-18 6:03am    
Anyone pls help me?

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