关于ASP.NET与数据库连接的问题

lonely9292 2011-03-01 04:06:57
本人在做一个添加学生信息的网页,在页面中有需要添加的学生信息项目,然后点击保存之后连接到数据库进行保存。但是现在有个问题就是一直没办法将数据保存到数据库中,即是每次都显示“设置失败”,而数据库中的信息却可以读取到网页中来。
烦请各位好心帮忙解决一二~


下面是涉及到的部分前台代码:
<tr>
<td align="right" valign="top">
<a style="text-decoration:none;" href="#" onclick="window.open('addstuInfo.aspx?stuBarCode=add','','width=340,height=371');">添加学生信息</a>

</td>
</tr>
<tr>
<td valign="top">
<asp:GridView ID="gvstuInfo"  runat="server" AutoGenerateColumns="False" Width="818px" AllowPaging="True" OnRowDeleting="gvstuInfo_RowDeleting">
<Columns>
<asp:BoundField HeaderText="学生姓名" DataField="stuName" />
<asp:BoundField HeaderText="学生性别" DataField="sex" />
<asp:BoundField HeaderText="学生类型" DataField="type" />
<asp:BoundField HeaderText="证件类型" DataField="certificateType" />
<asp:BoundField HeaderText="证件号码" DataField="certificate" />
<asp:BoundField HeaderText="联系电话" DataField="tel" />
<asp:BoundField DataField="email" HeaderText="E-mail" />
<asp:BoundField HeaderText="备注" DataField="remark" />
<asp:TemplateField HeaderText="修改">
<ItemTemplate>
<a href="#" style="text-decoration:none;" onclick="window.open('addstuInfo.aspx?stuBarCode=<%#Eval("stuBarCode") %>','','width=340,height=371')">修改</a>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" >
<ControlStyle Font-Underline="False" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1"  Font-Bold="True" />
<PagerStyle BackColor="#ABCEEB" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#ABCEEB" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White"  />
</asp:GridView>
<br />
</td>
</tr>

下面是后台代码:
public partial class stuManage_addstuInfo : System.Web.UI.Page
{
string barcode = "";
protected void Page_Load(object sender, EventArgs e)
{
barcode = Request.QueryString["stuBarCode"].ToString(); //获取对学生操作的方式
if (!IsPostBack) //判断是否是首次加载
{
bindDdlstuType();
//自定义方法绑定
if (barcode != "add") //判断是否是添加操作
{
this.Title = "修改学生信息";
txtPass.Enabled = false;
bindstuInfo();
}
else
{
this.Title = "添加学生信息";
this.txtstuBarCode.Text = bindBarcode();
}
}

}

public string bindBarcode()
{
//获取当前日期的年,月,日转换成字符串类型用于表示条形码
string date = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
//获取当前时间的小时,分钟转换成字符串类型用于表示条形码
string time = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
return date + time;  //返回一个10位的条形码
}
protected void btnSave_Click(object sender, EventArgs e)
{
string sBarcode = txtstuBarCode.Text;
string sPass = txtPass.Text;
string sName = txtstuName.Text;
string Sex = "";
if (radbtnMan.Checked)
{
Sex = "男";
}
else
{
Sex = "女";
}
string sType = ddlstuType.SelectedValue;
string CertType = ddlCertificateType.SelectedValue;
string Cert = txtCertificate.Text;
string Tel = txtTel.Text;
string Email = txtEmail.Text;
string Remark = txtRemark.Text;
string sql = "";
if (dataOperate.execSQL(sql))
{
if (barcode == "add")
{
sql = "insert into tb_stuInfo values('" + sBarcode + "','" + sPass + "','" + sName + "','" + Sex + "','" + sType + "','" + CertType + "','" +
Cert + "','" + Tel + "','" + Email + "','" + Remark + ")";
}
else
{
sql = "update tb_stuInfo set stuName='" + sName + "',sex='" + Sex + "',stuType='" + sType + "',certificateType='" + CertType + "',certificate='" +
Cert + "',tel='" + Tel + "',email='" + Email + "',remark='" + Remark + "' where stuBarCode='" + barcode + "'";
}
dataOperate.execSQL(sql);
Response.Write("<script>alert('设置成功!');window.opener.location.href=window.opener.location='stuInfo.aspx';window.close();</script>");
}
else
{
Response.Write("<script>alert('设置失败!')</script>");
}


}
public void bindDdlstuType()
{
string sql = "select * from tb_stuType";
ddlstuType.DataSource = dataOperate.getDataset(sql);
ddlstuType.DataTextField = "type";
ddlstuType.DataValueField = "id";
ddlstuType.DataBind();
}

public void bindstuInfo()
{
string sql = "select * from tb_stuInfo where stuBarCode='" + barcode + "'";
SqlDataReader sdr = dataOperate.getRow(sql);
sdr.Read();
txtstuBarCode.Text = sdr["stuBarcode"].ToString();
txtstuName.Text = sdr["stuName"].ToString();
txtRemark.Text = sdr["remark"].ToString();
txtTel.Text = sdr["tel"].ToString();
txtEmail.Text = sdr["email"].ToString();
txtCertificate.Text = sdr["certificate"].ToString();
if (sdr["sex"].ToString().Trim() == "男")
{
radbtnMan.Checked = true;
}
else
{
radbtnWoman.Checked = true;
}
ddlCertificateType.SelectedValue = sdr["certificateType"].ToString();
ddlstuType.SelectedValue = sdr["stuType"].ToString();
}
}


下面是涉及到的公共类函数:
public static bool execSQL(string sql)
{
//创建数据库连接
SqlConnection con = createCon();
//打开数据库连接
con.Open();
//创建SqlCommand对象
SqlCommand com = new SqlCommand(sql, con);
try
{
//执行SQL语句
com.ExecuteNonQuery();

}
catch (Exception e)
{
//返回布尔值False
return false;
}
finally
{
//关闭数据库连接
con.Close();
}
//返回布尔值True
return true;
}



本人觉得是不是那两个数据库的操作语句没有被执行而造成的。。
...全文
205 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
alan_219_2008 2011-03-03
  • 打赏
  • 举报
回复
我是说可以使用公共静态变量或者放session或者调方法时传值
lonely9292 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 alan_219_2008 的回复:]
设置公共静态变量
session
bindstuInfo(string barcode) 方法传值
[/Quote]

使用session来保存这个静态变量吗?具体怎么操作,能不能再说的详细点,本人在这方面比较空。。
alan_219_2008 2011-03-03
  • 打赏
  • 举报
回复
设置公共静态变量
session
bindstuInfo(string barcode) 方法传值
lonely9292 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 alan_219_2008 的回复:]
string barcode = "";
protected void Page_Load(object sender, EventArgs e)
{

string barcode = "";这个也不应该放外面 不然你想一下 页面一刷新 barcode就为空,那你后面的判断肯定又要出问题
[/Quote]

那如果string barcode = "";这个东西也放在里面的话,那不得在protected void btnSave_Click(object sender, EventArgs e)和 public void bindstuInfo()里面都在定义一次string barcode = "";?因为那两个模块里面都用到barcode的。。
alan_219_2008 2011-03-03
  • 打赏
  • 举报
回复
string barcode = "";
protected void Page_Load(object sender, EventArgs e)
{

string barcode = "";这个也不应该放外面 不然你想一下 页面一刷新 barcode就为空,那你后面的判断肯定又要出问题
alan_219_2008 2011-03-03
  • 打赏
  • 举报
回复
stuBarCode这个值是你根据前一个页面上传过来的
当然得在第一次加载stuManage_addstuInfo时取 不然你stuManage_addstuInfo页面一刷新Request.QueryString["stuBarCode"]就会报错
alan_219_2008 2011-03-03
  • 打赏
  • 举报
回复
barcode = Request.QueryString["stuBarCode"].ToString();
把这段话放到
if (!IsPostBack) //判断是否是首次加载
里面 不然页面刷新后barcode就肯定为null了
lonely9292 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wyq29 的回复:]
引用 14 楼 lonely9292 的回复:
引用 12 楼 wyq29 的回复:
断点调试 一清二楚!!

何必代码看半天

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用……
[/Quote]

小弟又修改调试了好久,仍然没有解决这个问题,烦请大侠再给个提示呀~
lonely9292 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wyq29 的回复:]
引用 14 楼 lonely9292 的回复:
引用 12 楼 wyq29 的回复:
断点调试 一清二楚!!

何必代码看半天

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用……
[/Quote]

不太懂没那个。。那如果出错的原因是“添加成功”后的那条语句丢失了stuBarCode的参数,那stuBarCode最开始的参数不是来自于前台代码中的“<a style="text-decoration:none;" href="#" onclick="window.open('addstuInfo.aspx?stuBarCode=add','','width=340,height=371');">添加学生信息</a>”的吗?为什么还跟后台代码中添加成功后返回的值有关系呢?
lonely9292 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 alan_219_2008 的回复:]
空指针
barcode = Request.QueryString["stuBarCode"].ToString(); 为null
[/Quote]

我是在stuInfo.aspx页面中点击了“添加学生信息”来打开的addstuInfo.aspx,此时应该有stuBarCode=='add'了,然后上面的那条语句中barcode可以得到这个'add'了呀,为什么还是空指针呢,难道是跟15楼的大牛所说的那样,在显示“设置成功”后打开的页面丢失了stuBarCode的参数吗?
alan_219_2008 2011-03-03
  • 打赏
  • 举报
回复
空指针
barcode = Request.QueryString["stuBarCode"].ToString(); 为null
wyq29 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 lonely9292 的回复:]
引用 12 楼 wyq29 的回复:
断点调试 一清二楚!!

何必代码看半天

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


……
[/Quote]

你已经测试出来了啊 很明显 你添加成功
Response.Write("<script>alert('设置成功!');window.opener.location.href=window.opener.location='stuInfo.aspx';window.close();</script>");

返回的是 'stuInfo.aspx' 没有了stuBarCode参数!
barcode = Request.QueryString["stuBarCode"].ToString(); 肯定报错!

当然执行到 pageload 就错了啊
lonely9292 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wyq29 的回复:]
断点调试 一清二楚!!

何必代码看半天
[/Quote]
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 16: protected void Page_Load(object sender, EventArgs e)
行 17: {
行 18: barcode = Request.QueryString["stuBarCode"].ToString(); //获取对学生操作的方式
行 19: if (!IsPostBack) //判断是否是首次加载
行 20: {



这是我在insert into 语句后加入断点后调试的结果,第18行出错,但是看这个提示,我却不知道具体错误要怎么解决了。。barcode应该是在点击了“添加学生信息”之后就等于“add”的了,不然页面标题也不会显示“添加学生信息”呀。但是还是提示那里出错。
lonely9292 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 rock870210 的回复:]
你insert 的Sql语句肯定有问题。
dataOperate.execSQL(sql);
这句中方法
public static bool execSQL(string sql)
{
//创建数据库连接
SqlConnection con = createCon();
//打开数据库连接
con.Open();
//创建SqlCommand对象
……
[/Quote]

遗憾的是,按照你说的方法并没有显示出错误的信息。。而且连“设置成功”和“设置失败”的提示都没有了。
wyq29 2011-03-03
  • 打赏
  • 举报
回复
断点调试 一清二楚!!

何必代码看半天
Rock870210 2011-03-03
  • 打赏
  • 举报
回复
你insert 的Sql语句肯定有问题。
dataOperate.execSQL(sql);
这句中方法
public static bool execSQL(string sql)
{
//创建数据库连接
SqlConnection con = createCon();
//打开数据库连接
con.Open();
//创建SqlCommand对象
SqlCommand com = new SqlCommand(sql, con);
try
{
//执行SQL语句
com.ExecuteNonQuery();

}
catch (Exception e)
{
//返回布尔值False
//return false;这里把异常屏蔽了。你肯定看不到。把这句话注释了。用下面那句
throw e;
}
finally
{
//关闭数据库连接
con.Close();
}
//返回布尔值True
return true;
}

再次运行会给你提示错误信息!
代码之城 2011-03-02
  • 打赏
  • 举报
回复
http://blog.csdn.net/Jamie2012/archive/2011/02/28/6214242.aspx

最近研究了一下asp.net 2008 连接sql2005的代码,终于能明白了。

首先环境

1、系统是XP sp3

2、编程工具是vs2008 asp.net(vb)

3、数据库是 sql2005 express版本

首先设置web.config 连接代码,把如下代码粘贴到里

<appSettings>
<add key="ConnectionString" value="server=localhost\sqlexpress;uid=sa;pwd=xxxxx;database=dataname"/>
</appSettings>

其次,在代码中编写代码。

1、在代码页面最上面写上

Imports System.Data.SqlClient

2、数据库连接代码

Dim sconnect As String
sconnect = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
Dim conn As SqlConnection
conn = New SqlConnection(sconnect)
conn.Open()

注意首先设置sql2005 express 设置sa登入名 和密码。授权管理数据库,还有设置数据库允许远程连接。


china wholesale
Rock870210 2011-03-02
  • 打赏
  • 举报
回复
string sql = "";
if (dataOperate.execSQL(sql)) {
if (barcode == "add")
{
sql = "insert into tb_stuInfo values('" + sBarcode + "','" + sPass + "','" + sName + "','" + Sex + "','" + sType + "','" + CertType + "','" +
Cert + "','" + Tel + "','" + Email + "','" + Remark + ")";
}
else
{
sql = "update tb_stuInfo set stuName='" + sName + "',sex='" + Sex + "',stuType='" + sType + "',certificateType='" + CertType + "',certificate='" +
Cert + "',tel='" + Tel + "',email='" + Email + "',remark='" + Remark + "' where stuBarCode='" + barcode + "'";
}
dataOperate.execSQL(sql);
Response.Write("<script>alert('设置成功!');window.opener.location.href=window.opener.location='stuInfo.aspx';window.close();</script>");
}
else
{
Response.Write("<script>alert('设置失败!')</script>");
}

你觉得你那句代码有意义吗?去执行一个空的sql语句。

if (barcode == "add")
{
sql = "insert into tb_stuInfo values('" + sBarcode + "','" + sPass + "','" + sName + "','" + Sex + "','" + sType + "','" + CertType + "','" +
Cert + "','" + Tel + "','" + Email + "','" + Remark + ")";
}
else
{
sql = "update tb_stuInfo set stuName='" + sName + "',sex='" + Sex + "',stuType='" + sType + "',certificateType='" + CertType + "',certificate='" +
Cert + "',tel='" + Tel + "',email='" + Email + "',remark='" + Remark + "' where stuBarCode='" + barcode + "'";
}
try
{
dataOperate.execSQL(sql);
Response.Write("<script>alert('设置成功!');window.opener.location.href=window.opener.location='stuInfo.aspx';window.close();</script>");

}catch(Exception ex)
{
Response.Write("<script>alert('设置失败!错误原因:"+ex.Message+"')</script>");
}
lonely9292 2011-03-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cyq1162 的回复:]
推荐你用 CYQ.Data 数据框架 来操作数据库,就不会有这么让人头疼的代码产生了。
[/Quote]

请问一下,这个是一个软件吗?用来操作sql数据库的?
lonely9292 2011-03-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jamie2012 的回复:]
http://blog.csdn.net/Jamie2012/archive/2011/02/28/6214242.aspx

最近研究了一下asp.net 2008 连接sql2005的代码,终于能明白了。

首先环境

1、系统是XP sp3

2、编程工具是vs2008 asp.net(vb)

3、数据库是 sql2005 express版本

首先设置web.……
[/Quote]

其实数据库应该是和网页链接了的,不然我的网页也不会能读出数据库中的各项数据,问题是我网页中的添加数据的功能却没办法成功,没办法传到数据库中保存起来。也就是说现在数据的传送是单方向的。
加载更多回复(6)

62,046

社区成员

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

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

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

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