2017-02-11 46 views
0

我试图运行该应用程序,但它显示2错误。红线在Label1.Txt和Label2.Txt下。请参阅下面的代码在执行当前Web请求期间生成了未处理的异常asp net

 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"; 
      }  
     } 
    } 
} 
+0

有“文本”被宣布为两个标签下面? – Faegy

+0

你使用网页表单吗? – Saif

+0

是的,我正在使用网页形式,文本将只显示在提交按钮请求 –

回答

0

您需要更改这个

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

这样:

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

但这只是将修复异常并不会显示的短信..

,如果你想显示的消息那么您需要在HTML中声明标签:

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

,然后更改您的代码如下:

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"; 
     }  
    } 
} 

并删除代码

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

非常感谢,它的工作非常感谢 –

+0

请标记为答案,,,我的荣幸帮助你 – Saif

0

label1label2被声明为object型。 object没有属性Text,所以这个错误即将到来。你可能需要施放它们。

相关问题