Hi I'm new at this I have created a database Person with (ID,Name,address) in MS SQL
an d I have the following code

protected void Button1_Click(object sender, EventArgs e)
    {
   
        
        System.Data.SqlClient.SqlConnection conn =new System.Data.SqlClient.SqlConnection();
        
        conn.ConnectionString ="integrated security=SSPI;data source=SQL Server Name;" +"persist security info=False;initial catalog=northwind";
        try
        {
            conn.Open();
            
            string sql = "DELETE FROM Person WHERE ID = 4"; //just as an example is number 4 taken
            
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.ExecuteNonQuery();
            cmd.ExecuteScalar();
            // Insert code to process data.
        }
        catch (Exception ex)
        {
           
        }
        finally
        {
            conn.Close();
        }

    }

I work with Visual Studio 2005. it runs OK no error message but it does not deletes th Person with ID 4 .

Recommended Answers

All 3 Replies

First your question should be in C# Forum http://www.daniweb.com/forums/forum61.html
Second, it should raise exception but you don't show it "blank catch block" modify it to

catch (Exception ex)
{
MessageBox.Show(ex.Message);           
}

I believe that the problem in the connection string. make sure it connects to the server by debugging your code.

First, set a breakpoint at line conn.Open(); , execute the line and check conn.State. It should be Open (or ConnectionState.Open) if your connection string is ok. Otherwise, recheck connection string like Ramy said.

After you get your code work at that point, you have

cmd.ExecuteNonQuery();
cmd.ExecuteScalar();

Since DELETE is not a query, remove cmd.ExecuteScalar(); line.

Check your connection string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.