2017-04-10 60 views
0

我試圖插入和更新數據使用相同的按鈕。我創建了方法(uniqueEmail())來檢查表中是否存在電子郵件地址。如果電子郵件沒有預設,使用這種方法我試圖插入數據。 這裏是我的代碼,請糾正我在哪裏出錯。插入和更新函數工作在單擊按鈕在C#

public partial class _Default : System.Web.UI.Page 
{ 
    SqlConnection con = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=register;Integrated Security=True"); 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    public void Button1_Click(object sender, EventArgs e) 
    { 
     con.Open(); 
     SqlCommand cmd = con.CreateCommand(); 
     cmd.CommandType = CommandType.Text; 
     if (uniqueEmail()==true) 
     { 
      cmd.CommandText = "update registeruser set email='" + TextBox1.Text + "', password='" + TextBox2.Text + "' where email='" + TextBox1.Text + "'"; 
     } 
     else 
     { 
      cmd.CommandText = "insert into registeruser values('" + TextBox1.Text + "', '" + TextBox2.Text + "')"; 
     } 

     cmd.ExecuteNonQuery(); 
     con.Close(); 

    } 
    public bool uniqueEmail() 
    { 
     string stremail; 
     string querye = "select count(email) as email from registeruser"; 
     SqlCommand cmd = new SqlCommand(querye, con); 
     SqlDataReader dr; 
     dr = cmd.ExecuteReader(); 
     while (dr.Read()) 
     { 
      try 
      { 
       stremail = dr["email"].ToString(); 
       return(stremail != "0"); 
       if (stremail != "0") 
       { 
        //errlblemail.Text = "email already exist"; 
        return false; 
       } 

      } 
      catch (Exception e) 
      { 
       string message = "error"; 
       message += e.Message; 
      } 
      finally 
      { 
       dr.Close(); 
      } 
     } 

     return true; 


    } 
} 
+0

代碼看起來不錯,現在是什麼問題? –

+0

@nirmala你的代碼很好。請寫出什麼是錯誤? –

+0

如果我使用現有的emailid進行註冊,而不是更新數據,則會再插入一次。 – Nirmala

回答

0

您需要檢查特定的EMAILID的數量,而不是總數量。

修改爲下面的代碼:

public static bool uniqueEmail(string email) 
{ 
     string stremail; 
     string querye = "select count(email) as email from register where 
     email = '" + email + "'"; 
     //Remaining Code 
} 

public static void Button1_Click(object sender, EventArgs e) 
{ 
     con.Open(); 
     SqlCommand cmd = con.CreateCommand(); 
     cmd.CommandType = CommandType.Text; 
     if (uniqueEmail(TextBox1.Text)) == true) 
     //Remaining Code 
    } 
+0

非常感謝@ Boney現在的工作。但我懷疑爲什麼我們使用這個語句「return(stremail!=」0「);」就好像使用條件一樣。請解釋我對.net新手。 – Nirmala

+0

它是一個C#表達式,它的計算結果是一個布爾值。它基本上等於下面的一段代碼: 'if(stremail!=「0」) return true; //計數(電子郵件)不是零。電子郵件已存在於數據庫中更新 否則 返回false; //計數(電子郵件)爲零。新郵件。做插入.' – Boney

0

@nirmala就應該更換方法

public void EmailCheck() 
{ 
    string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString; 
    SqlConnection con = new SqlConnection(constring); 
    SqlCommand cmd = new SqlCommand("Select * from EmailSignUp where EmailId= @EmailId", con); 
    cmd.Parameters.AddWithValue("@EmailId", this.txtEmail.Text); 
    con.Open(); 
    SqlDataReader dr = cmd.ExecuteReader(); 
    while (dr.Read()) 
    { 
     if (dr.HasRows == true) 
     { 
      MessageBox.Show("EmailId = " + dr[5].ToString() + " Already exist"); 
      txtEmail.Clear(); 
      break; 
     } 
    } 

} 
0

兩件事情需要做

  1. 傳遞電子郵件ID同時呼籲

    如果(uniqueEmail()==真)

if (uniqueEmail(TextBox1.Text)==true) 
  • 而在uniqueEmail方法chenage查詢()包括其中如下條件

    public bool uniqueEmail(email) 
    { 
    string stremail; 
    string querye = "select count(email) as email from registeruser where email='" + email + "'"; 
    
    //your remaining code 
    } 
    
  • 0

    您好修女代碼是隻有正確的你需要把where子句找到已經存在於數據庫中的電子郵件ID。

    public partial class _Default : System.Web.UI.Page 
    { 
        SqlConnection con = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=register;Integrated Security=True"); 
    
        protected void Page_Load(object sender, EventArgs e) 
        { 
    
        } 
    
        public void Button1_Click(object sender, EventArgs e) 
        { 
         con.Open(); 
         SqlCommand cmd = con.CreateCommand(); 
         cmd.CommandType = CommandType.Text; 
         if (uniqueEmail()==true) 
         { 
          cmd.CommandText = "update registeruser set email='" + TextBox1.Text + "', password='" + TextBox2.Text + "' where email='" + TextBox1.Text + "'"; 
         } 
         else 
         { 
          cmd.CommandText = "insert into registeruser values('" + TextBox1.Text + "', '" + TextBox2.Text + "')"; 
         } 
    
         cmd.ExecuteNonQuery(); 
         con.Close(); 
    
        } 
        public bool uniqueEmail() 
        { 
         string stremail; 
         string querye = "select count(email) as email from registeruser where email = '" +TextBox1.Text+ "'"; 
         SqlCommand cmd = new SqlCommand(querye, con); 
         SqlDataReader dr; 
         dr = cmd.ExecuteReader(); 
         while (dr.Read()) 
         { 
          try 
          { 
           stremail = dr["email"].ToString(); 
           return(stremail != "0"); 
           if (stremail != "0") 
           { 
            //errlblemail.Text = "email already exist"; 
            return false; 
           } 
    
          } 
          catch (Exception e) 
          { 
           string message = "error"; 
           message += e.Message; 
          } 
          finally 
          { 
           dr.Close(); 
          } 
         } 
    
         return true; 
    
    
        } 
    }