Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
*/
                con.Open();

cmd = new SqlCommand("INSERT INTO student_details (name, father, mother, surname, phone, email, grno, course, rollno, yearofjoin,state,blood,address,pic)  VALUES  ('" + name.Text + "','" + father.Text + "','" + mother.Text + "','" + surname.Text + "','" + phone.Text + "','" + email.Text + "','" + grno.Text + "','" + course.Text + "','" + rollno.Text + "','" + Convert.ToDateTime(yearofjoin.Text) + "','" + comboBox1sem.Text + "','" + comboBox1blood.Text + "','" + address.Text + "',@pic)", con);
                MemoryStream stream = new MemoryStream();
                pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] pic = stream.ToArray();
                    cmd.Parameters.AddWithValue("@pic", pic);
                j = cmd.ExecuteNonQuery();



update code

cmd = new SqlCommand(@"UPDATE student_details SET  name='" + uname.Text + "',father='" + ufather.Text + "',mother='" + umother.Text + "',surname='" + usurname.Text + "',phone='" + uphone.Text + "',email='" + uemail.Text + "',course='" + ucourse.Text + "',rollno='" + urollno.Text + "',yearofjoin='" + uyearofjoin.Text + "',state='"+comboBox1usem.Text+"',blood='"+comboBox1ublood.Text+"',address='"+uaddress.Text+"',pic=@img where (grno='" + ugrno.Text + "')", con);
                MemoryStream stream = new MemoryStream();
                pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] pic = stream.ToArray();
                cmd.Parameters.AddWithValue("@img", pic);
                j = cmd.ExecuteNonQuery();

                if (j > 0)
                {
                    MessageBox.Show("Record Updated Successfully...");
                }
                ClearingText();
                con.Close();


What I have tried:

can anyone rewrite the insert code and update code for me
Posted
Updated 17-Oct-17 2:32am
Comments
Maciej Los 16-Oct-17 15:20pm    
Never use such as sql commands, because they are SqlInjection vulnerable!
ZurdoDev 17-Oct-17 10:55am    
No.

C#
cmd = new SqlCommand("INSERT INTO student_details (name, father, mother, surname, phone, email, grno, course, rollno, yearofjoin,state,blood,address,pic)  VALUES  ('" + name.Text + "','" + father.Text + "','" + mother.Text + "','" + surname.Text + "','" + phone.Text + "','" + email.Text + "','" + grno.Text + "','" + course.Text + "','" + rollno.Text + "','" + Convert.ToDateTime(yearofjoin.Text) + "','" + comboBox1sem.Text + "','" + comboBox1blood.Text + "','" + address.Text + "',@pic)", con);

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
Comments
Richard Deeming 17-Oct-17 12:41pm    
In this case, it almost certainly is the solution to the question! :)
Patrice T 17-Oct-17 14:41pm    
Agreed :)
Formatting the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]

try like this
SqlCommand cmd = new SqlCommand("INSERT INTO student_details (name, father, mother, surname, phone, email, grno, course, rollno, yearofjoin,state,blood,address,pic)  VALUES  ( @name, @father, @mother, @surname, @phone, @email, @grno, @course, @rollno, @yearofjoin,@state,@blood,@address,@pic)");
cmd.Parameters.AddWithValue("@name",name.Text);
cmd.Parameters.AddWithValue("@father",father.Text);
cmd.Parameters.AddWithValue("@mother",mother.Text);
cmd.Parameters.AddWithValue("@surname",surname.Text);
cmd.Parameters.AddWithValue("@email",email.Text);
cmd.Parameters.AddWithValue("@grno",grno.Text);
cmd.Parameters.AddWithValue("@course",course.Text);
cmd.Parameters.AddWithValue("@rollno",rollno.Text);
cmd.Parameters.AddWithValue("@yearofjoin",Convert.ToDateTime(yearofjoin.Text));
cmd.Parameters.AddWithValue("@state",comboBox1sem.Text);
cmd.Parameters.AddWithValue("@blood",comboBox1blood.Text);
cmd.Parameters.AddWithValue("@address",address.Text);
cmd.Parameters.AddWithValue("@pic", pic);


use DateTime.TryParseExact Method [^] to convert datetime from a known string format.
 
Share this answer
 
As per my knowledge you cannot pass datetime in inline query, use stored procedure which a better way to perform save and update.
 
Share this answer
 
Comments
Richard Deeming 17-Oct-17 12:42pm    
Completely wrong. So long as you use parameters, you can pass any value you want to an inline query, without having to write a stored procedure.

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