首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

求教帮忙看一下下面的代码,注册页面里的“注册”按钮的Click事件,点击后不能向用户表里插入信息!

2012-04-09 
求教各位高手帮忙看一下下面的代码,注册页面里的“注册”按钮的Click事件,点击后不能向用户表里插入信息!!!!

求教各位高手帮忙看一下下面的代码,注册页面里的“注册”按钮的Click事件,点击后不能向用户表里插入信息!!!!
各位大侠,我用 Visual web developer 2005 速成版,链接的是外部sql sever 2005 数据库,里面有张Register表,存放新注册用户信息,在“注册”按钮的click事件里写了代码,但点击注册的时候不能向表里插入数据,求教高手帮忙看一下下面的代码!!!!!!


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class zc : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected bool Check(string text)//判断实现
  {
  if (text.Contains("<") || text.Contains(">") || text.Contains("'") ||
  text.Contains("//") || text.Contains("\\"))//检查字串
  {
  return true;//返回真
  }
  else
  {
  return false;//返回假
  }
  }

  protected void zhc_Click(object sender, EventArgs e)
  {
  if (Check(TextBox1.Text) || Check(TextBox2.Text) || Check(TextBox4.Text) ||
  Check(TextBox5.Text) || Check(TextBox6.Text))//判断
  {
  Label1.Text = "用户信息中不能够包含特殊字符如<,>,',//,\\等,请审核";//输出信息
  }
  else
  {
  try
  {
  SqlConnection con =
  new SqlConnection("server='(local)';database='Register';uid='sa';pwd='sa'");//建立连接
  con.Open();//打开连接
  string strsql = "insert into Register(username)values('" + TextBox1.Text + "')";
  string check = "select * from register where username='" + TextBox1.Text + "'"; //创建适配器
  SqlDataAdapter da = new SqlDataAdapter(check, con); //创建数据集
  DataSet ds = new DataSet(); //填充数据集
  da.Fill(ds, "table");
  if (da.Fill(ds, "table") > 0) //判断同名
  {
  Label1.Text = "注册失败,有相同用户名";  
  }
  else
  {
  SqlCommand cmd = new SqlCommand(strsql, con); //创建执行对象
  cmd.ExecuteNonQuery();//执行SQL
  Label1.Text = "注册成功,请牢记您的信息";
  }

  }
  catch
  {
  Label1.Text = "出现错误信息,请返回给管理员";//抛出异常
  }

  }

  }
}


[解决办法]
else
{
SqlCommand cmd = new SqlCommand(strsql, con); //创建执行对象
cmd.ExecuteNonQuery(); //执行SQL
Label1.Text = "注册成功,请牢记您的信息";
}
如果你的是进入了else就说明是成功的,如果没有进入,你就把具体错误说一下。
[解决办法]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class zc : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected bool Check(string text) //判断实现
{
if (text.Contains("<") || text.Contains(">") || text.Contains("'") ||
text.Contains("//") || text.Contains("\\")) //检查字串
{
return true; //返回真
}
else
{
return false; //返回假
}
}

protected void zhc_Click(object sender, EventArgs e)
{
if (Check(TextBox1.Text) || Check(TextBox2.Text) || Check(TextBox4.Text) ||
Check(TextBox5.Text) || Check(TextBox6.Text)) //判断
{
Label1.Text = "用户信息中不能够包含特殊字符如<,>,',//,\\等,请审核"; //输出信息
}
else
{
try
{
SqlConnection con =
new SqlConnection("server=.;database=Register;uid=sa;pwd=sa"); //建立连接 ,不需要单引号,去掉
con.Open(); //打开连接
string strsql = "insert into Register(username) values('" + TextBox1.Text + "')"; //表名Register和数据库名一样的??
string check = "select * from Register where username='" + TextBox1.Text + "'"; //创建适配器
SqlDataAdapter da = new SqlDataAdapter(check, con); //创建数据集
DataSet ds = new DataSet(); //填充数据集
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0) //判断同名
{
Label1.Text = "注册失败,有相同用户名";
}
else
{
SqlCommand cmd = new SqlCommand(strsql, con); //创建执行对象
cmd.ExecuteNonQuery(); //执行SQL
Label1.Text = "注册成功,请牢记您的信息";
}

}
catch
{
Label1.Text = "出现错误信息,请返回给管理员"; //抛出异常
}

}

}
}
[解决办法]
为什么说一些编程培训“坑人”呢,只要我们认真想一下

C# code
  catch  {  Label1.Text = "出现错误信息,请返回给管理员";     //抛出异常  } 

热点排行