I have a problem and I would like your help. I have in my page an edit button and textboxes. I want to edit one single record from the database to these textboxes. I wrote the following code, when I click the edit button then I have the results. The results load in my textboxes. But when I try again to run the page with the same ID for second time then I have this error ‘Specified cast is not valid.’ What’s going wrong? I used int ID=3; to get the 3rd record in this example
protected void Button2_Click(object sender, EventArgs e)
{
DataClassesDataContext cxt = new DataClassesDataContext();
USER_TABLE aChar = cxt.USER_TABLEs.Single(c => c.ID == 3); //gets a single record
fname2.Text = aChar.FIRST_NAME;
lname2.Text = aChar.LAST_NAME;
pob2.Text = aChar.PLACE_OF_BIRTH;
pom2.Text = aChar.PLACE_OF_MARRIAGE;
education2.Text = aChar.EDUCATION;
occupation2.Text = aChar.OCCUPATION;
pod2.Text = aChar.PLACE_OF_DEATH;
String str = aChar.DATE_OF_BIRTH.ToString();
String str1 = aChar.DATE_OF_MARRIAGE.ToString();
String str2 = aChar.DATE_OF_DEATH.ToString();
Why are you doing
if (!Page.IsPostBack)in your event handler code?I’d say that normally an event is only raised upon post-back…
I rarely see IsPostBack used in anything other than PageLoad.