簡體   English   中英

我想在網頁上的一個級別打印存儲過程的消息

[英]I want to print message of stored procedure in a level on web page

查詢:
ALTER 過程 [dbo].[sp_test1] (@Username nvarchar(50),@Password nvarchar(50),@MobileNo nvarchar(20)) as begin if exists (select MobileNo from test1 where MobileNo = @MobileNo and Username != @用戶名)選擇“已存在的移動不存在”作為味精否則如果存在(從 test1 中選擇用戶名,其中用戶名 = @Username 和 MobileNo != @MobileNo)選擇“已經存在的用戶名”作為味精,否則如果存在(從 test1 中選擇用戶名,MobileNo,其中Username = @Username and MobileNo=@MobileNo) 選擇“Already UserName & Mobile No Existed”作為msg else begin insert into test1 values(@Username,@Password,@mobileno) 選擇“Profile Created”作為msg end end

C# 代碼:公共部分類 _Default:System.Web.UI.Page { string str = ConfigurationManager.ConnectionStrings["Test"].ConnectionString; protected void Page_Load(object sender, EventArgs e) {

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

}

總而言之,您的代碼中有 2 個功能:

  • ValidateForm():它只是檢查用戶輸入是空的還是空的
  • Save():它只是向數據庫插入一條新記錄

然后,如果輸入是重復數據,您希望顯示消息以通知用戶。

好吧,你需要一個函數檢查重復數據。 我們稱之為 CheckDulicate()

最后,我的建議是這樣的:

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

如果您在打印文本框的消息時只有問題,您可以通過兩種方式進行 -
1. MessageBox.Show("你的消息在這里");
2. 設置顯示特定控件的錯誤文本消息的驗證規則。

第二種方法是在將數據保存到數據庫之前驗證任何控件的正確方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM