我想在一个水平网页上打印存储过程的消息

问题描述:

SQL查询:
ALTER过程[DBO] [sp_test1] (@UserName为nvarchar(50),@密码为nvarchar(50),@ MobileNo nvarchar的(20)) 作为 开始 如果存在(从TEST1选择MobileNo其中 MobileNo = @MobileNo和用户名!= @用户名) 选择“已手机号码存在的”味精 否则,如果存在(选择test1的地方 用户名用户名= @Username和MobileNo!= @MobileNo) 选择'已存在用户名'作为msg 否则如果存在(从test1选择用户名,MobileNo Ë 用户名= @用户名和MobileNo = @ MobileNo) 选择 '用户名已经移动&没有存在的' 味精 其他 开始 INSERT INTO test1的 值(@用户名,密码@,@ mobileno) 选择 '档案创建'如MSG 端 端我想在一个水平网页上打印存储过程的消息

C#代码: 公共部分类_Default:System.Web.UI.Page { 字符串str = ConfigurationManager.ConnectionStrings [ “测试”]的ConnectionString; 保护无效的Page_Load(对象发件人,EventArgs的) {

} 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     if (ValidateForm()) 
     { 

      Save(); 
     } 
    } 
    private void Save() 
    { 
     SqlConnection con = new SqlConnection(str); 
     SqlCommand cmd = new SqlCommand("sp_test1", con); 
     con.Open(); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.AddWithValue("Username",TextBox1.Text); 
    cmd.Parameters.AddWithValue("Password",TextBox2.Text); 
    cmd.Parameters.AddWithValue("MobileNo",TextBox3.Text); 
    cmd.ExecuteNonQuery(); 

    if (count > 0) 
    { 
     lbluser.Text = "Username is already exists"; 
    } 
    else 
    { 
     lblmsg.Text = "Registered Successfully"; 
    } 
    con.Close(); 
    Response.Redirect("Default.aspx"); 


} 
private bool ValidateForm() 
{ 
    bool ret = true; 
    { 

     if (string.IsNullOrEmpty(TextBox1.Text)) 
     { 
      ret = false; 
      lbluser.Text = "Please Enter Username"; 
     } 
     else 
     { 
      lbluser.Text = ""; 
     } 
     if (string.IsNullOrEmpty(TextBox2.Text)) 
     { 
      ret = false; 
      lblpwd.Text = "Please Enter Password"; 
     } 
     else 
     { 
      lblpwd.Text = ""; 
     } 
     if (string.IsNullOrEmpty(TextBox3.Text)) 
     { 
      ret = false; 
      lblmob.Text = "Please Enter Mobile Number"; 
     } 
     else 
     { 
      lblmob.Text = ""; 
     } 
     return ret; 

    } 

} 
protected void Button2_Click(object sender, EventArgs e) 
{ 
    Response.Redirect("Login.aspx"); 
} 

}

+0

寻求调试帮助的问题(“为什么不是这个代码工作?”)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者无益。 –

+0

指定要为其检查重复值的“文本框”。 – mmushtaq

+0

可能无助于解决您的问题,但您应该在参数名称前添加一个“@”。 ([见这里](https://*.com/questions/10245510/is-it-necessary-to-add-a-in-front-of-an-sqlparameter-name)) – MatSnow

总结一下,你有2个功能代码:

  • ValidateForm():它只是检查用户输入为空或空
  • 保存():它只是向数据库插入新记录

然后,如果输入是重复数据,则要显示消息以通知用户。

好的,你需要一个函数检查重复数据。让我们把它叫做CheckDulicate()

最后,我建议是这样的:

if (ValidateForm()) 
{ 
    if(CheckDulicate() == false) 
    { 
     Save(); 
    } 
    else 
    { 
     // display the message you want 
     // MessageBox.Show("...."); 
    } 
} 

如果您在打印的文本消息唯一的问题,你可以做到这一点有两种方式 -
1.消息框。显示(“你的信息在这里”);
2.设置用于显示特定控件的错误文本消息的验证规则。

第二种方式是在将数据保存到数据库之前验证任何控件的正确方法。