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

I tried to perform insert operation using odbccommand.executenonquery
it returns object reference error at same time it insert record to table

what is reason of exception ?

What I have tried:

VB
Dim cmd as OdbcCommand

cmd = new odbcCommand (s_sql, Con) 

 If Cnn.State = ConnectionState.Closed Then Cnn.Open()

  cmd.ExecuteNonQuery()

cmd.dispose() 
Posted
Updated 1-Oct-20 22:18pm

1 solution

We can't tell from that fragment: we have no access to your DB, the data you are passing, or the rest of your code.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

But ... from that fragment I'd say that you have a more serious problem that you need to address as a matter of urgency through your whole app: SQL Injection.
You are passing the INSERT command to SQL via a str8ing, which means that values you want to INSERT are part of the string. That's a very, very bad idea!

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Comments
yogeshysankar 3-Oct-20 3:44am    
@OriginalGriff I do debugging. While debug where exception has been thrown at that time I Copied that query and try to execute in sql express but query is perfectly execute in sql express, there is no issue with query.
My question is when I use command.executenonQuery sometimes it throw exception still query get execute , record get inserted in table then why objectreference error occures??
OriginalGriff 3-Oct-20 3:57am    
The object reference error isn't coming from SQL, it's com8ing from your C# code somewhere - and the debugger shows you the exact line it occured on. It's then up to you to look at eteh objects on that line and find out exactly which one(s) are null and causing the error to be detected. Then you use the debugger to find out why it's null when you didn't think it should be.

That needs your code running in the debugger - which we can't do - and your user inputs - which we don't have - and your data base - which we can't access.
yogeshysankar 3-Oct-20 5:07am    
thanks for reply.. Error is occured at line cmd.executeNonQuery where Connection is not null and cmd also not null I checked that . I am using single connection through out project . I cant provide my official database sorry. I just want to know all possibilities . But how it would happen error and insertion at same time, because record get inserted ??

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