last day (23 days later) » 

1:03 AM
0
Q: C# ExecuteNonQuery

LeKerkiiiMy query will execute the first time in a switch case loop, but during the second case nothing happens with it everything is written inside of a for loop, it manages to add the first query into the database properly but after that it doesn't string sQuery = string.Format("'{0}','{1}','{2}','{3}...

 
Why are you assigning this to an adapter if you execute the command directly and dispose it before leaving the switch? This part seems unnecessary. You should be able to do command = new SqlCommand(sql, cnn); command.ExecuteNonQuery();.
As for why it doesn't do anything in the second case, have you debugged your code? For instance, is Properties.Settings.Default.HoF true ?
You are also likely to be prone to sql injection by using string concatenation/string.Format to build your SQL. You should look into parameters, although debugging should tell you if this is your current problem or not.
 
user12175610
I used the debugger, it jumps into the if loop but at the command command.ExecuteNonQuery(); it just stops.
 
And you say, everything is inside a for-loop. I hope you're not using for (int i = 0; i < ...; i++) and then using switch inside to decide what to do?
By "just stops", do you mean that it hangs or does it leave your method? Do you have a try/catch surrounding all of this that swallows exceptions?
 
N.B. It's unclear how case 0 would ever work either since there appears to be no value set for sQuery in this instance. It would help generally if you showed a bit more of your code (e.g. including your loop and any try/catch blocks which wrap around this code)
 
user12175610
Yes i have a try/catch surrounding it, and with "just stops" i mean that it jumps to my exetion from the catch, the first insert was execute successfully
 
1:03 AM
well if you're getting an exception it really makes sense to tell us what it is - we can't guess! Obviously that message will be a big clue to the cause of the problem.
 
user12175610
then i tried to copy paste the first case into my second case but it stops working at the moment where the debugger reaches the second ExecuteNonQuery
 
user12175610
I use the Switch case inside a for loop, what exactly is bad about this?
 
user12175610
I just edited the post to contain more code so you can see a bigger part of it
 
Why are you even using a SqlDataAdapter here? You can use a SqlCommand directly. Also what about the SqlConnection, is this a shared instance that you might be using in other places at the same time?
 
user12175610
I´m new to SqlConnections in C# so I watched some Youtube Videos and there they used an Adapter, too. In all honesty, I have no clue what it does.And yes the SqlConnection is a shared instance containing the connection string
 
1:03 AM
"I use the Switch case inside a for loop, what exactly is bad about this?" It's utterly unnecessary. You could as well just leave the loop and switch out and just execute the two statements sequentially. That would be more readable, even.
"and there they used an Adapter, too" let's leave the Adapter alone, for a while. It doesn't break your code, I think, although it is superfluent here. Please add the stacktrace and message of your exception.
 
Refactor your code. Remove the SqlDataAdapter, its not necessary. Use SqlParameters, see How can I add user-supplied input to an SQL statement?, and Exploits of a Mom. Wrap the SqlConnection instance in a using block. Remove the loop, just execute 1 SqlCommand and then another sequentially.
 
Does the table have a Primary Key? A Primary key will only allow one row with the same Family_name. SQL Server an Insert will not change the database if the Primary Key already exists. You would need to use Update. ExecuteNonQuery returns an integer indicating the number of rows changed. If you do an Insert and zero is returned then it indicates the Primary key is already in the database. Then you must use Updated. Likewise, if you use Update and zero is returned then it indicates the Primary key is not in the table and you must use Insert.
 
@jdweng ordinarily, inserting a row with an existing primary key value would result in an exception, not just a 0 affected rows result.
@LeKerkiii "In all honesty, I have no clue what it does" ...don't use things you don't understand. Read the Microsoft documentation and tutorials if you don't know what something is. And maybe also read this for wider understanding of what you're doing in this situation and why it's bad.
 
@ADyson : Wrong, Wrong, Wrong!!! SQL Server returns zero for ExecuteNonQuery.
 
@jdweng no, it's true. By default you get a primary key violation error - just as you would if you ran it in SQL directly. See for yourself. I just ran it a moment ago... .NET 4.7, SQL 2016. Table setup: imgur.com/a/B22M5l6, C# code: imgur.com/a/eXbHNC9, result: imgur.com/a/ODrBTxF . Have you by any chance been setting IGNORE_DUP_KEY = ON in your primary key indexes, or something similar? That would produce the result you're describing.
 

last day (23 days later) »