写了一个网页,是一个用户注册页面,功能就是将用户在文本框中输入的信息存储到数据库中去,出了点问题..
-----------------------------上面省略一些命名空间usingSystem.Data.SqlClient;publicpartialclassr...
-----------------------------上面省略一些命名空间
using System.Data.SqlClient;
public partial class regist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btregist_Click(object sender, EventArgs e)//点击注册按钮执行下面
{ //建立到数据库的连接
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=ACER-PC\\SQLEXPRESS;Initial Catalog=workinfo;User ID=sa;Password=123;";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into seinfo(uid,name,password,email,department,phone,address,position,description)values(@uid,@name,@password,@email,@department,@phone,@address,@position,@description);";
cmd.Connection = conn;
SqlParameter parauid = new SqlParameter("@uid",SqlDbType.Char,15);
SqlParameter paraname = new SqlParameter("@name",SqlDbType.Char,10);
SqlParameter parapassword = new SqlParameter("@password",SqlDbType.NChar,15);
SqlParameter paraemail = new SqlParameter("@email",SqlDbType.Char,20);
SqlParameter paradepartment = new SqlParameter("@department",SqlDbType.Char, 10);
SqlParameter paraphone = new SqlParameter("@phone",SqlDbType.Char,20);
SqlParameter paraaddress = new SqlParameter("@address",SqlDbType.Char,10);
SqlParameter paraposition = new SqlParameter("@position",SqlDbType.Char,10);
SqlParameter paradescription = new SqlParameter("@description",SqlDbType.NVarChar,500);
parauid.Value =useridtext.Text ;
paraname.Value =nametext.Text;
paraemail.Value = emailtext.Text;
paradepartment.Value = departmenttext.Text;
paraphone.Value = phonetext.Text;
paraaddress.Value = addresstext.Text;
paraposition.Value = positiontext.Text;
paradescription.Value = descriptiontext.Text;
cmd.Parameters.Add(parauid);
cmd.Parameters.Add(paraname);
cmd.Parameters.Add(parapassword);
cmd.Parameters.Add(paraemail);
cmd.Parameters.Add(paradepartment);
cmd.Parameters.Add(paraphone);
cmd.Parameters.Add(paraaddress);
cmd.Parameters.Add(paraposition);
cmd.Parameters.Add(paradescription);
int record = cmd.ExecuteNonQuery();
Response.Write(record.ToString());
conn.Close();
conn.Dispose();
}
}
我注册页面中用于用户名输入的文本框组件ID名字更改过,就叫useridtext,但是运行就是错误提示上下文中不存在useridtext,后来试过前面加上This 改成了this.useridtext,输入命令的时候也有自动补充完整的提示了,但是再运行又说"regist"不包含useridtext的定义并且找不到一个接受类型为"regist"的第一个参数的扩展方法"useridtext"。请问如何解决? 展开
using System.Data.SqlClient;
public partial class regist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btregist_Click(object sender, EventArgs e)//点击注册按钮执行下面
{ //建立到数据库的连接
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=ACER-PC\\SQLEXPRESS;Initial Catalog=workinfo;User ID=sa;Password=123;";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into seinfo(uid,name,password,email,department,phone,address,position,description)values(@uid,@name,@password,@email,@department,@phone,@address,@position,@description);";
cmd.Connection = conn;
SqlParameter parauid = new SqlParameter("@uid",SqlDbType.Char,15);
SqlParameter paraname = new SqlParameter("@name",SqlDbType.Char,10);
SqlParameter parapassword = new SqlParameter("@password",SqlDbType.NChar,15);
SqlParameter paraemail = new SqlParameter("@email",SqlDbType.Char,20);
SqlParameter paradepartment = new SqlParameter("@department",SqlDbType.Char, 10);
SqlParameter paraphone = new SqlParameter("@phone",SqlDbType.Char,20);
SqlParameter paraaddress = new SqlParameter("@address",SqlDbType.Char,10);
SqlParameter paraposition = new SqlParameter("@position",SqlDbType.Char,10);
SqlParameter paradescription = new SqlParameter("@description",SqlDbType.NVarChar,500);
parauid.Value =useridtext.Text ;
paraname.Value =nametext.Text;
paraemail.Value = emailtext.Text;
paradepartment.Value = departmenttext.Text;
paraphone.Value = phonetext.Text;
paraaddress.Value = addresstext.Text;
paraposition.Value = positiontext.Text;
paradescription.Value = descriptiontext.Text;
cmd.Parameters.Add(parauid);
cmd.Parameters.Add(paraname);
cmd.Parameters.Add(parapassword);
cmd.Parameters.Add(paraemail);
cmd.Parameters.Add(paradepartment);
cmd.Parameters.Add(paraphone);
cmd.Parameters.Add(paraaddress);
cmd.Parameters.Add(paraposition);
cmd.Parameters.Add(paradescription);
int record = cmd.ExecuteNonQuery();
Response.Write(record.ToString());
conn.Close();
conn.Dispose();
}
}
我注册页面中用于用户名输入的文本框组件ID名字更改过,就叫useridtext,但是运行就是错误提示上下文中不存在useridtext,后来试过前面加上This 改成了this.useridtext,输入命令的时候也有自动补充完整的提示了,但是再运行又说"regist"不包含useridtext的定义并且找不到一个接受类型为"regist"的第一个参数的扩展方法"useridtext"。请问如何解决? 展开
3个回答
展开全部
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 zhuce : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox6.Text = "";
TextBox5.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["blogConnectionString2"].ConnectionString;
SqlConnection conn = new SqlConnection(conStr);
// 构造插入的SQL语句
string sql = "INSERT INTO [users](username, psw, age,sex,email)VALUES (@username, @psw, @age,@sex,@email)";
SqlCommand comm = new SqlCommand(sql, conn);
// 设置Command对象的参数
comm.Parameters.Add("@username", SqlDbType.NVarChar, 50);
comm.Parameters.Add("@psw", SqlDbType.VarChar, 50);
comm.Parameters.Add("@age", SqlDbType.NChar, 4);
comm.Parameters.Add("@sex", SqlDbType.NChar, 4);
comm.Parameters.Add("@email", SqlDbType.NVarChar, 50);
// 设置Command对象的参数值
comm.Parameters["@username"].Value = TextBox1.Text;
comm.Parameters["@psw"].Value = TextBox2.Text;
comm.Parameters["@age"].Value = TextBox6.Text;
comm.Parameters["@sex"].Value = DropDownList1.SelectedValue;
comm.Parameters["@email"].Value = TextBox5.Text;
conn.Open();
try
{
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write("数据库插入记录时出错,出错信息为:<br>" + ex.Message);
return;
}
finally
{
conn.Close();
}
//Response.Write("<script language='javascript'>alert('恭喜您,注册成功!');location.href='Login.aspx'</script>");
Response.Redirect("~/login.aspx");
}
}
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 zhuce : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox6.Text = "";
TextBox5.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["blogConnectionString2"].ConnectionString;
SqlConnection conn = new SqlConnection(conStr);
// 构造插入的SQL语句
string sql = "INSERT INTO [users](username, psw, age,sex,email)VALUES (@username, @psw, @age,@sex,@email)";
SqlCommand comm = new SqlCommand(sql, conn);
// 设置Command对象的参数
comm.Parameters.Add("@username", SqlDbType.NVarChar, 50);
comm.Parameters.Add("@psw", SqlDbType.VarChar, 50);
comm.Parameters.Add("@age", SqlDbType.NChar, 4);
comm.Parameters.Add("@sex", SqlDbType.NChar, 4);
comm.Parameters.Add("@email", SqlDbType.NVarChar, 50);
// 设置Command对象的参数值
comm.Parameters["@username"].Value = TextBox1.Text;
comm.Parameters["@psw"].Value = TextBox2.Text;
comm.Parameters["@age"].Value = TextBox6.Text;
comm.Parameters["@sex"].Value = DropDownList1.SelectedValue;
comm.Parameters["@email"].Value = TextBox5.Text;
conn.Open();
try
{
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write("数据库插入记录时出错,出错信息为:<br>" + ex.Message);
return;
}
finally
{
conn.Close();
}
//Response.Write("<script language='javascript'>alert('恭喜您,注册成功!');location.href='Login.aspx'</script>");
Response.Redirect("~/login.aspx");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询