我正在尝试将一个文本值分配给一个catch块中的文字我尝试了这个代码,但在例外情况下它将null值传递给 Literal1.Text
public partial class leadsmg : System.Web.UI.Page
{...
[WebMethod]
[ScriptMethod]
public static void Savecmp(Cmpdet user)
{
leadsmg ld = new leadsmg();
string user_Id = HttpContext.Current.Session["id"].ToString();
SqlConnection conn = new SqlConnection(HttpContext.Current.Session["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("insert into companydet (cname,caddr,cweb,cpno,ccd,uid,cad)values(@Cname,@Caddr,@Cweb,@Cpnno,@Cdate,@Uid,getdate())");
try
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Cname", user.Cname);
cmd.Parameters.AddWithValue("@Caddr", user.Caddr);
cmd.Parameters.AddWithValue("@Cweb", user.Cweb);
cmd.Parameters.AddWithValue("@Cpnno", user.Cpno);
cmd.Parameters.AddWithValue("@Cdate", user.Cdateadd);
cmd.Parameters.AddWithValue("@Uid", user_Id);
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception e)
{
conn.Close();
ld.Literal1.Text = "Company already exits";//here its passing null value to Literal1.Text
}
}.....}
leadmg 是 Savecmp 存在的主要方法。
如何解决这个问题?