Jump to content

A little SQL project in C#

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
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]

Attached Files



#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
What is the exception? Post your delete statement.

#3
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
I'm posting whole delete code because I think it's not the problem in delete statement:

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
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
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.

#5
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
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