我正在尝试运行该应用,但它显示2个错误。红线位于Label1.Txt和Label2.Txt下。请参阅以下代码
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 :(得分:0)
label1
和label2
被声明为object
类型。 Text
没有属性object
,因此会出现此错误。你可能需要施放它们。
答案 1 :(得分: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; }