Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Not working Properly.....
Here is the code
C#
private void button1_Click(object sender, EventArgs e)
       {
           if (textBox1.Text == "")
           {
               MessageBox.Show("Please Enter Student's Name","Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
           else
               try
               {
                   string cs = @"Data Source=NADEEM-PC\SQLEXPRESS;Initial Catalog=School Managment System;Integrated Security=True";
                   SqlConnection con = new SqlConnection(cs);
                   SqlCommand cmd = new SqlCommand(("DELETE FROM [Students Records] WHERE Reg.No ='"+ textBox1.Text+"'"), con);
                   cmd.Connection = con;
                   con.Open();
                   cmd.ExecuteNonQuery();
                   con.Close();
                   MessageBox.Show("Deleted");
               }
               catch (Exception ex)
               {

                   MessageBox.Show(ex.Message);
               }

It runs properly but when we enter reg.no in txtbox1 and click button it gives an error Reg.no is unique identifier in database reg,no is primary key of table please help me???
Posted
Updated 3-Mar-14 1:54am
v2
Comments
King Fisher 3-Mar-14 7:59am    
can you show the Reg.No value ?

Reg.No is PK and therefore it cannot be "null".

With your SQL Command you pass "" as string for Reg.No and this is not allowed.

Go with SQL Parameter:

C#
SqlCommand cmd = new SqlCommand("DELETE FROM [Students Records] WHERE [Reg.No] = @pRegNo, con);
cmd.Parameters.AddWithValue("@pRegNo", textBox1.Text);
 
Share this answer
 
Comments
Member 10564850 3-Mar-14 8:06am    
can you please write whole delete statment from my program with correct syntax??
Member 10564850 3-Mar-14 8:18am    
It given an error cannot convert data type into int????
midnight_ 3-Mar-14 8:21am    
Rewrite like this:

cmd.Parameter.AddWithValue("@pRegNr", Convert.ToInt32(textBox1.Text));
Normally, we would say "show us the exact error message" - because it's important, and it normally contains a lot of useful information.

But...in this case I suspect it's that there is a foreign key relationship with another table, and SQL is complaining that deleting this record would cause other records to refer to a non-existent row. There are two ways to get round this:
1) Manually delete all dependant rows in other tables first, then delete this row.
2) Use CASCADE deletes to make SQL do it for you: CASCADE in SQL Server with example[^]

But please, don't do it like that! Do not 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. Use Parametrized queries instead.
 
Share this answer
 
Comments
Member 10564850 3-Mar-14 8:17am    
no by deleting reg.no there is no impact on any other tables
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Please Enter Student's Name","Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
try
{
string cs = @"Data Source=NADEEM-PC\SQLEXPRESS;Initial Catalog=School Managment System;Integrated Security=True";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(
SQL
"DELETE FROM [Students Records] WHERE [Reg.No] = @Reg, con);
cmd.Parameters.AddWithValue("@Reg", textBox1.Text

);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Deleted");
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
 
Share this answer
 

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