2017-04-10 62 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; 
    
    
        } 
    }