user2972112
user2972112

Reputation: 131

Why the row is not being inserted into the DB? C#

Last update

I think I found why this is happening. Everytime I try to debug my application, a new Database file is created by Visual Studio on the debug folder. This file is deleted when my app closes, so every change I make during runtime is deleted. I don't know how to change this, but I will post it when I figure it out.

**

Edited to add more info

**

I created a class in C# to handle the Database operations. I have a method to read from the DB that works just fine. But when I try to insert rows, nothing happens. There's no error or exception, the code runs just as it's supposed to, reaches the "return true" statement, but when I check the DB after, I haven't got any new data. The DB is empty by now, so it doesn't have any conflicts with any unique fields. The id field is not auto-generated, so that's not the problem either.

Here's the code:

public bool saveCity(City c)
    {
        string query = "INSERT INTO [dbo].[cities] ([Id], [Name], [Latitude], [Longitude], [Radius], [LastUpdate])" +
            " VALUES (@Id, @Name, @Latitude, @Longitude, @Radius, @LastUpdate);";
        try
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    conn.Open();
                    cmd.Parameters.AddWithValue("@Id", c.Id);
                    cmd.Parameters.AddWithValue("@Name", c.Name);
                    cmd.Parameters.AddWithValue("@Latitude", c.Latitude);
                    cmd.Parameters.AddWithValue("@Longitude", c.Longitude);
                    cmd.Parameters.AddWithValue("@Radius", c.Radius);
                    cmd.Parameters.AddWithValue("@LastUpdate", c.Update);
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        catch (Exception)
        {
            return false;
        }
    }

The connString is defined as a class field:

connString = Properties.Settings.Default.DBConnectionString;

I don't have any triggers in my DB, or nothing that could delete my rows after inserted. At the moment, it has only one table, cities, which looks like this:

DB screen capture

The code of the City class:

class City
    {
        public int Id;
        public string Name;
        public int Radius;
        public double Latitude;
        public double Longitude;
        public DateTime Update;

        public City()
        {
            Id = -1;
            Name = "";
            Radius = 0;
            Latitude = 0;
            Longitude = 0;
            Update = DateTime.Now;
        }


}

It's my first time working with C# DB connections, so probably there's something really obvious and stupid I'm missing, but it's driving me crazy.

Upvotes: 4

Views: 540

Answers (3)

user2972112
user2972112

Reputation: 131

Finally, I found the solution. Visual Studio creates a new instance of the DB everytime you try to debug. Someone posted a workaround for this issue, so here's the link:

Can I commit changes to actual database while debugging C# in Visual Studio?

Upvotes: 1

i486
i486

Reputation: 6563

Change double to decimal, specify DB type in Parameters.Add and change INSERT INTO ... VALUES (@id,...) syntax to INSERT INTO ... SELECT @id,.... Maybe one of them is the problem but it is better to change all.

Upvotes: 0

Hugh
Hugh

Reputation: 454

Include the data type and a question mark instead of @ in front of the parameter names as well. eg:

string query = @"INSERT INTO cities (Id, name, coord_x, coord_y, radius, updated_at)" + " VALUES (?id, ?name, ?lat, ?lng, ?rad, ?update)";

 cmd.Parameters.Add("?id", MySqlDbType.Int32).Value = c.Id      

etc..

Hope this helps

Upvotes: 0

Related Questions