Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void btnSave_Click(object sender, EventArgs e)
       {

cmd.CommandText = "insert into Student values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','" + textBox15.Text + "','" + textBox16.Text + "','" + textBox17.Text + "','" + textBox18.Text + "','" + textBox19.Text + "','" + listBox1.SelectedItem + "','" + comboBox1.SelectedItem.ToString() + "','" + comboBox2.SelectedItem.ToString() + "','" + comboBox3.SelectedItem.ToString() + "','" + textBox20.Text + "','" + comboBox4.SelectedItem.ToString() + "','" + checkBox5.Text + "','" + textBox21.Text + "','" + textBox22.Text + "','" + textBox23.Text + "'," + int.Parse(textBox24.Text) + "," + int.Parse(textBox25.Text) + ",'" + checkBox3.Text + "','" + comboBox5.SelectedItem.ToString() + "','" + textBox26.Text + "','" + comboBox6.SelectedItem.ToString() + "','" + textBox27.Text + "','" + checkBox4.Text + "'," + int.Parse(textBox28.Text) + "," + int.Parse(textBox29.Text) + ",'" + checkBox1.Text + "','" + checkBox2.Text + "','" + listBox2.SelectedItem + "')";

            cmd.Connection = con;

            cmd.ExecuteNonQuery();

             clear();
Posted
Updated 19-Apr-13 10:49am
v3
Comments
José Amílcar Casimiro 19-Apr-13 16:51pm    
Can you read your code?
Richard C Bishop 19-Apr-13 16:51pm    
Holy cow man, that is the most dangerous concatentated string I have ever seen. You really ought to consider using parameterized queries. What you have now will leave you open to SQL injection. Also, there is not enough info here to help you. Where is the exception being thrown? What line?


Your error means you are using something that has not been instantiated.
chintu fron cali 19-Apr-13 16:52pm    
iam new to .net, please correct my code if u find any mistakes.
Dave Kreskowiak 19-Apr-13 16:59pm    
Find any mistakes?? THE ENTIRE THING IS A MISTAKE!!

Serisouly, read the articles I linked to and you'll get your answer.
chintu fron cali 19-Apr-13 17:05pm    
ok,thanks for sending artical

YEOW!! MY EYES!! WHAT AN UNHOLY ABOMINATION!!

Scrap that garbage and rewrite it as a parameterized query. If you don't know what I'm talking about, read these[^]. You'll find out why what you wrote is so horribly bad.

Hint: Being able to debug the code MUCH easier is one reason.

At least one of those expressions is returning null. It's impossible for anyone to tell you which one because of the way you wrote this. I can tell you it's not a textbox because the Text property never returns null.
 
Share this answer
 
v3
Comments
Richard C Bishop 19-Apr-13 16:57pm    
My sentiments exactly. Nice helpful response.
Sergey Alexandrovich Kryukov 19-Apr-13 17:21pm    
Agree, my 5, but the most important aspect is SQL injection. Please see my answer.
There is a funny example, by the way...
—SA
Richard C Bishop 19-Apr-13 17:24pm    
That is funny. I will forever use the legendary tale of 'ol Bobby Tables.
Sergey Alexandrovich Kryukov 19-Apr-13 17:27pm    
:-)
As far as I know, first brought to CodeProject by Espen Harlinn who told me he would not mind if I use it, too... :-)
—SA
I'm fully agree with Dave's Solution 1 and want to add a not on an important: not only obtaining a query using string concatenation is ugly, it also ineffective (because strings are immutable), and, much more importantly, it is utterly unsafe: you keep the doors open to a well-known exploit called SQL injection: http://en.wikipedia.org/wiki/SQL_injection[^].

The article referenced above explains the importance parametrized statements.

This is a very nice example of SQL injection: http://xkcd.com/327/[^].

Please also see my past answers for further detail:
hi name is not displaying in name?[^],
EROR IN UPATE in com.ExecuteNonQuery();[^].

—SA
 
Share this answer
 
As to the exception:

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Good luck,
—SA
 
Share this answer
 
Comments
chintu fron cali 19-Apr-13 17:43pm    
Thanks for sharing all ideas iam tring to solve
Sergey Alexandrovich Kryukov 19-Apr-13 18:57pm    
My pleasure, but don't forget SQL injection.
—SA
chintu fron cali 19-Apr-13 18:09pm    
Hello everyone, thanks for all your help. I got it.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900