[ATTACH]4552[/ATTACH]
4 replies to this topic
#1
Posted 09 February 2012 - 03:32 PM
Hello again. I'm trying to learn MSSQL in C#.NET. I am working with Windows Forms Application and I am trying to implement 3 basic statements of SQL in that project. I have managed Insert and Select statement very good but I have problems with Delete Statement. I will put my project here because I will not troll with huge code. What I want is to see fields in my table and type specific user and specific car to delete from that table. Program throws me an exception for Delete button so I am out of ideas for that button. Please help me.
[ATTACH]4552[/ATTACH]
[ATTACH]4552[/ATTACH]
|
|
|
#2
Posted 10 February 2012 - 09:29 AM
What is the exception? Post your delete statement.
#3
Posted 11 February 2012 - 11:28 AM
I'm posting whole delete code because I think it's not the problem in delete statement:
Exception is: ImageShack® - Online Photo and Video Hosting
but there is another problem, when I want to delete fields with same values like "Jovan Perišić Hummer" and "Jovan Perišić Hummer" SQL Server 2008 R2 Enterprise gives me exception: ImageShack® - Online Photo and Video Hosting this exception is the same if I want to delete single row not multiple rows
String connectionString = "Server=localhost;Database=Vjezba;Trusted_Connection=Yes;";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM Employees", connection);
command.Connection = connection;
command.CommandText = "SELECT * FROM Tablica1";
command.CommandType = CommandType.Text;
command.CommandText = "DELETE FROM Tablica1 WHERE Ime=" + textBox1.Text + " AND Prezime=" + textBox2.Text + " AND Auto=" + textBox3.Text;
try
{
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Exception is: ImageShack® - Online Photo and Video Hosting
but there is another problem, when I want to delete fields with same values like "Jovan Perišić Hummer" and "Jovan Perišić Hummer" SQL Server 2008 R2 Enterprise gives me exception: ImageShack® - Online Photo and Video Hosting this exception is the same if I want to delete single row not multiple rows
#4
Posted 11 February 2012 - 01:37 PM
The first thing I would worry about is SQL injection. But if this is a simple practice example, it would be fine. Is the field a string (varchar)? I can't see the photos on my phone, so I can't see the error. Secondly, you should NOT use names as the primary key.
As for the characters, just escape them with an additional ', so ' would become '' in the string.
As for the characters, just escape them with an additional ', so ' would become '' in the string.
#5
Posted 12 February 2012 - 12:52 PM
tnx on help but i have fixed my problem in application, i just needed to include scalar variable Ime, Prezime and Auto but what I want is to select some row at dataGridView control and to delete only that row, when I'm using my code which I have now if I delete row which has the same values like some other row it would delete those rows too. I don't want that. But there is still funny question about SQL Server 2008 R2. Why SQL wouldn't allow me to delete those rows which are "copied"?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account



Back to top









