菜鸟求救~~~~

岁月静好_cai 2012-09-17 08:19:39
以下是我的代码,要想判断页面是否第一次加载后执行Button_Click中的事件,也就是将页面信息加到数据库中:
public string text;

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
//Response.Write("<script language=javascript>alert('11111!')</script>");



}

}


protected void RadioButton_CheckedChanged(object sender, EventArgs e)
{

var rb = (RadioButton)sender;
if (rb.Checked == true)
{
var fraction = rb.Attributes["value"];
var type = this.GetType();
var fInfo = type.GetField(rb.GroupName);
fInfo.SetValue(this, fraction);
}
}

public int insert()
{
string sql = string.Format("insert UserTable values('{0}','{1}','{2}','{3}','{4}')", TextBox1.Text, TextBox2.Text, TextBox3.Text, text, TextBox4.Text);

int i = DBConnection.inserttable(sql);
return i;

}

public DataTable GetData()
{
string sql = "select UserName from UserTable where='" + TextBox1.Text + "'";
SqlConnection con = new SqlConnection("Data Source=OEM-20120908EIP;Initial Catalog=Information Network;Integrated Security=True");
DataTable datatable = new DataTable();
SqlDataAdapter sad = new SqlDataAdapter(sql, con);
sad.Fill(datatable);

return datatable;

}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = GetData();
string str = dt.Rows[0][0].ToString();
Response.Write("<script language=javascript>alert('" + str + "')</script>");
if (dt.Rows.Count == 0)
{
Response.Write("<script language=javascript>alert('用户名已被注册!')</script>");
TextBox1.Text = null;


}
else
{
int i = insert();
if (i == 1)
{
Response.Write("<script language=javascript>alert('注册成功!')</script>");


}

}

//insert();

}
...全文
129 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
岁月静好_cai 2012-09-18
  • 打赏
  • 举报
回复
问题已解决,谢谢各位
岁月静好_cai 2012-09-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

string sql = "select UserName from UserTable where UserName='" + TextBox1.Text + "'";
我猜的你的字段名
[/Quote]

这个已经解决了,谢谢
beyond_me21 2012-09-17
  • 打赏
  • 举报
回复
string sql = "select UserName from UserTable where UserName='" + TextBox1.Text + "'";
我猜的你的字段名
岁月静好_cai 2012-09-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

目测,查询语句貌似有问题select UserName from UserTable where=

应该是 where [字段]='条件'

其实你断点跟踪一下什么都清楚了、
[/Quote]
恩,这个已经改过来了
岁月静好_cai 2012-09-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

debug 一下吧。


尤其是注册的时候,下面的代码会有异常的。

string str = dt.Rows[0][0].ToString()
[/Quote]

恩恩,这里提示在位置0处没有任何行,是什么情况呢?该怎么改??
  • 打赏
  • 举报
回复
目测,查询语句貌似有问题select UserName from UserTable where=

应该是 where [字段]='条件'

其实你断点跟踪一下什么都清楚了、
机器人 2012-09-17
  • 打赏
  • 举报
回复
debug 一下吧。


尤其是注册的时候,下面的代码会有异常的。

string str = dt.Rows[0][0].ToString()
岁月静好_cai 2012-09-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

问题是?
[/Quote]

帮忙看看代码的问题所在吧,执行的时候注册信息无法添加到数据库中
岁月静好_cai 2012-09-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhoukang0916 的回复:]

没明白你的意思。。。
[/Quote]

我知道了,不需要IsPostBack去判断页面是否要加载,可是为什么注册页面的信息没有加到数据库中呢?我的代码哪儿有问题么??
岁月静好_cai 2012-09-17
  • 打赏
  • 举报
回复
就是if(!IsPostBack)方法体中不知道该写什么了……
PandaIT 2012-09-17
  • 打赏
  • 举报
回复
没明白你的意思。。。
机器人 2012-09-17
  • 打赏
  • 举报
回复
问题是?

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧