I’m trying to write a web application with some database interaction in C#.NET. I’m getting an odd exception that I can’t figure out where it’s coming from. I found the method that it is coming from and the code is as follows:
protected void cmdDelete_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(@"Server=myname-PC\SQLEXPRESS;Database=dbNames;Trusted_Connection=True;"))
{
try
{
conn.Open();
SqlCommand comm = new SqlCommand("DELETE FROM Names WHERE FirstName=@FirstName AND LastName=@LastName", conn);
char[] delims = new char[1];
delims[0] = ' ';
string[] names = cblNames.SelectedItem.Text.Split(delims);
string fname = names[0];
string lname = names[1];
comm.Parameters.Add(new SqlParameter("@FirstName", fname));
comm.Parameters.Add(new SqlParameter("@LastName", lname));
comm.ExecuteNonQuery();
conn.Close();
cblNames.ClearSelection();
LoadTable();
}
catch (Exception ex)
{
Response.Write("Delete Table: "+ex.Message);
}
}
}
Any help is appreciated!
The problem that I was having was that I wasn’t getting the value on postback because I was leaving this little peace of code out of the page_load function:
It was an elementary lapse in knowledge. I have been developing in .NET since this question and I realize how horribly simple it is to make mistakes in a new language. Thank you everyone for dealing with the past noob me.