简体   繁体   中英

an unhandled exception was generated during the execution of the current web request asp net

I am trying to run the app but it showing 2 errors. The red line is under Label1.Txt and Label2.Txt. Refer to the following code

        public object Label1 { get; private set; }
        public object Label2 { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            var username = HttpContext.Current.Session["username"];

            if (username == null)
            {
                Response.Redirect("login.aspx");
            }    
        }

        protected void submitt_Click(object sender, EventArgs e)
        {
            try
            {   con.Open();
                SqlCommand command = new SqlCommand("insert into [Students] values ('" + ID.Text + "','" + FN.Text + "', '" + LN.Text + "', '" + Country1.Text + "', '" + gender.SelectedValue + "',  '" + email.Text + "', '" + passportNo.Text + "', '" + PlaceOfIssue.Text + "', '" + issue.Text + "',  '" + Expiry.Text + "', '" + VisaNo.Text + "', '" + VisaExpiry.Text + "', '" + EmiratesNo.Text + "', '" + EmiaratesExpiry.Text + "');", con);
                command.ExecuteNonQuery();
                con.Close();
                Label1.Text = "Uploaded Successfully"; 
            }
            catch (Exception ex)
            {
                Label2.Text = "Same data cannot be submitted again"; 
            }    
        }
    }
}

label1 and label2 are declared as object type. There is no property Text for object so this error is coming. You may need to cast them.

you need to change this

  public object Label1 { get; private set; }
  public object Label2 { get; private set; }

to this:

  public string Label1 { get; private set; }
  public string Label2 { get; private set; }

but this just will fix the exception and will not display message..

if you want to display message in label then you need to declare label in HTML like:

  <asp:label id="Label1" runat="server" />
  <asp:label id="Label2" runat="server" />

and then change your code to this:

    protected void submitt_Click(object sender, EventArgs e)
    {
        try
        {   con.Open();
            SqlCommand command = new SqlCommand("insert into [Students] values ('" + ID.Text + "','" + FN.Text + "', '" + LN.Text + "', '" + Country1.Text + "', '" + gender.SelectedValue + "',  '" + email.Text + "', '" + passportNo.Text + "', '" + PlaceOfIssue.Text + "', '" + issue.Text + "',  '" + Expiry.Text + "', '" + VisaNo.Text + "', '" + VisaExpiry.Text + "', '" + EmiratesNo.Text + "', '" + EmiaratesExpiry.Text + "');", con);
            command.ExecuteNonQuery();
            con.Close();
            Label1.Text = "Uploaded Successfully"; 
        }
        catch (Exception ex)
        {
            Label2.Text = "Same data cannot be submitted again"; 
        }    
    }
}

and remove the following from code

public string Label1 { get; private set; }
public string Label2 { get; private set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM