Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have used the following code:-

string sql = "insert into reg_table(name, gender, phone, country) values('" + name + "', '" + gender + "', " + phone + ", '" + country + "')";

            SqlCommand MyCmd = new SqlCommand(sql, conn);




Now, i want to test if the data is inserted in the database or not. If the data is inserted then it will show me a messagebox and say "data inserted" or "not inserted"...??
can anybody help?

Thanks and Regards
Akiii
Posted

To add to Sandeep's good answer, wrap the operation into try..catch block. In case of syntax errors, trigger raised errors etc you will have an exception at client side and obviously then no row is inserted...
 
Share this answer
 
Comments
Akiii123 22-Mar-11 9:50am    
@mika wendelius...
how will i wrap the code and raise trigger.....?
can u give me an example please...?

Akiii
Wendelius 22-Mar-11 10:01am    
What I meant is that a trigger defined on the table can cause an exception by calling RAISERROR. When the error is raised, you will receive an exception at the client side which you should handle properly (much like if the statement has syntax errors).
Akiii123 22-Mar-11 23:44pm    
i shd wrap that in try-catch block then....?
can you give me some good links where i can study stored procedures and triggers....i really want to learn in detail....
thanks for ur answer...
much appreciated..

Akiii
Wendelius 23-Mar-11 13:57pm    
You could start from here: http://msdn.microsoft.com/en-us/library/ms189626.aspx
Akiii123 24-Mar-11 0:00am    
thanks for the link...

Akiii
Well, use ExecuteNonQuery to execute your command.
As, ExecuteNonQuery: For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

Details here: SqlCommand.ExecuteNonQuery Method[^]

Thus, if one row of data would be inserted it would return 1 and then based on this value show the message as per your need.
 
Share this answer
 
Comments
Ankit Rajput 22-Mar-11 7:54am    
It's a good idea to check the return value if a function returns something. It will increase the performance as well in your case.
Akiii123 22-Mar-11 8:29am    
yes with ExecuteNonQuery its working fine.....
thank you very much for the prompt reply....

Thanks and Regards
Akiii
Sandeep Mewara 22-Mar-11 8:34am    
Good to know.

:thumbsup:
Akiii123 22-Mar-11 9:51am    
@ankit.....
thanks for the answer....
if the function returns void then?

Akiii
Wendelius 22-Mar-11 9:09am    
Good answer, 5 :)

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