deleting record from a sql server database

  • Thread starter Thread starter Joesoft11a
  • Start date Start date
J

Joesoft11a

Guest
I just trying to delete a record from a database. I'm not getting any errors and the breaks I put in seem to be returning the results. So I fail to under stand why this doesn't work. More then likely it's something I missed in the code.

When a user clicks on a choice in a listbox. Then clicks a button and then deleted that info from the database where = listbox1.text is.


private void btnDelete_Click(object sender, EventArgs e)
{

try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = @"Data Source=.\SQLEXPRESS; Initial Catalog=mlhelper;Trusted_Connection = true;";

conn.Open();

string dName = listBox1.Text;

string cmdd = "delete from games where shortname=@Gname ";
string t;


using (SqlCommand cm = new SqlCommand(cmdd, conn))
{

cm.Parameters.AddWithValue("@Gname", dName);

int rows = cm.ExecuteNonQuery();


if(rows == 1)
{
t = " Completed ";
}
else
{
t = " Not Completed ";
}

}

conn.Close();
MessageBox.Show("Game Deleted! "+ t + dName,Application.ProductName);
}
}
catch(Exception rr)
{
MessageBox.Show("Error Deleting Game! : " + rr.Message, Application.ProductName);
}

Loadgames();

//Ask to delete json file.

}
}

The if statement is what I use to make sure that the record is deleted. How ever I get the "Not completed" is displayed. So I have no idea. I think I just have something missing or the code is missing something.

Joe







http://www.df-barracks.com Delta Force Barracks
http://www.starfiresoft.com Starfire Software

Continue reading...
 
Back
Top