初学C#.NET
一个注册的程序,程序运行是正常,注册完也会显示注册成功,可在数据库表中没有刚注册的用户信息,怎么办??
程序为
private void BT_submit_Click(object sender, System.EventArgs e)
{
   string connstr="server=127.0.0.1;database=aspnet;uid=sa;pwd=111";
   string strsql="select count(*)from UserInfo where UserID like'"+TB_UserName.Text+"'";
   SqlConnection Conn=new SqlConnection(connstr);
   SqlCommand mycommand=new SqlCommand(strsql,Conn);
   Conn.Open();
   int flag=(int)mycommand.ExecuteScalar();
   Conn.Close();
   if(flag==0)
 {

    string selectstr="insert into UserInfo(UserID,Pwd,Sex,Email,Website)values('"+TB_UserName.Text+"','"+TB_Pwd.Text+"','"+RBL_Sex.SelectedItem.Value+"','"+TB_Email.Text+"','"+WebSite+"',getdate())";
   SqlCommand addcommand=new SqlCommand(selectstr,Conn);
   Conn.Open();

   Conn.Close();
   Session["username"]=TB_UserName.Text;
   Session["Sex"]=RBL_Sex.SelectedItem.Value;
   Label7.Text="注册成功,请先登录!";
}
 else 
 {
   Label7.Text="对不起,该用户名已经存在,请选择其他用户名!";
   TB_UserName.Text="";
   TB_Pwd.Text="";
   TB_Pswd.Text="";
}
}  private void BT_reset_Click(object sender, System.EventArgs e)
{
Response.Redirect("Register.aspx");
}

解决方案 »

  1.   

    string selectstr="insert into UserInfo(UserID,Pwd,Sex,Email,Website)空格values('"+TB_UserName.Text+"','"+TB_Pwd.Text+"','"+RBL_Sex.SelectedItem.Value+"','"+TB_Email.Text+"','"+WebSite+"',getdate())";addcommand.ExecuteScalar()
      

  2.   

    SqlCommand addcommand=new SqlCommand(selectstr,Conn);
    此语句创建的command根本就没有执行
      

  3.   

    if(flag==0)
    {
    string selectstr="insert into UserInfo(UserID,Pwd,Sex,Email,Website)values('"+TB_UserName.Text+"','"+TB_Pwd.Text+"','"+RBL_Sex.SelectedItem.Value+"','"+TB_Email.Text+"','"+WebSite+"',getdate())";
    SqlCommand addcommand=new SqlCommand(selectstr,Conn);
    Conn.Open();

    addcommand..ExecuteNonQuery();
       
    Conn.Close();
    Session["username"]=TB_UserName.Text;
    Session["Sex"]=RBL_Sex.SelectedItem.Value;
    Label7.Text="注册成功,请先登录!";
    }
      

  4.   

    insert into UserInfo(UserID,Pwd,Sex,Email,Website)values('"+TB_UserName.Text+"','"+TB_Pwd.Text+"','"+RBL_Sex.SelectedItem.Value+"','"+TB_Email.Text+"','"+WebSite+"',getdate())"这个语句也有问题,目标字段只有5个,可你写入字段确有6个??
      

  5.   

    1.语法上少个空格
     string selectstr="insert into UserInfo(UserID,Pwd,Sex,Email,Website)空格values('"+TB_UserName.Text+"','"+TB_Pwd.Text+"','"+RBL_Sex.SelectedItem.Value+"','"+TB_Email.Text+"','"+WebSite+"',getdate())";2.逻辑上没有执行命令对象   
    SqlCommand addcommand=new SqlCommand(selectstr,Conn);