0

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 .

4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by mail2saion
0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.