数据处理那一块老是出错?
这是提交后它报的错...但是这些基本上都处理了呢?原代码是:
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="C#" Debug="true"%>
<script language="C#" runat="server">
string strconn;
string querystr;
SqlConnection nwconn;
SqlDataAdapter stuadapter;
DataSet stuset=new DataSet();
void Page_Load()
{
ConnectData();
querystr="select * from datainfo " ;
stuadapter=new SqlDataAdapter(querystr,nwconn);
stuadapter.Fill(stuset,"datainfo");
if(!Page.IsPostBack)
{
BindGrid();
}
}
void ConnectData()
{
strconn="user id=sa;password=sql;database=datac;";
nwconn=new SqlConnection(strconn);
}
void BindGrid()
{
stugrid.DataSource=stuset;
stugrid.DataBind();
}
void Button1_click(Object sender,EventArgs E)
{
SqlConnection conn = new SqlConnection("Persist Security Info=false;database=datac;server=.;user id=sa;pwd=sql");
//String time_str=DateTime.Now().ToString();
String insertCmd="insert into datainfo(name,user_id,adress,pre_water,pre_air,c_water,c_air,p_water,p_air,time)
values(@name,@user_id,@adress,@pre_water,@pre_air,@c_water,@c_air,@p_water,@p_air,@time)";
SqlCommand istCmd = new SqlCommand(insertCmd, conn);
istCmd.Parameters.Add("@name",SqlDbType.VarChar,50).Value=name.Text;
istCmd.Parameters.Add("@user_id",SqlDbType.VarChar,20).Value=user_id.Text;
istCmd.Parameters.Add("@adress",SqlDbType.VarChar,20).Value=adress.Text;
istCmd.Parameters.Add("@pre_water", SqlDbType.Float, 50).Value = pre_water.Text;
istCmd.Parameters.Add("@pre_air", SqlDbType.Float, 50).Value = pre_air.Text;
istCmd.Parameters.Add("@c_water", SqlDbType.Float, 50).Value = c_water.Text;
istCmd.Parameters.Add("@c_air", SqlDbType.Float, 50).Value = c_air.Text;
istCmd.Parameters.Add("@p_water", SqlDbType.Float, 50).Value = p_water.Text;
istCmd.Parameters.Add("@c_air", SqlDbType.Float, 50).Value = c_air.Text;
istCmd.Parameters.Add("@time", SqlDbType.DateTime).Value =time.Text;
try
{
conn.Open();
istCmd.ExecuteNonQuery();
conn.Close();
Response.Write
(" &nb
sp; &n
bsp; 添加成功,系统将在
5 秒后自动返回后台主页面");
Response.Write("<meta HTTP-EQUIV=REFRESH CONTENT=5;URL=query.aspx>");
Response.Write("<br><br><br><br><br><br><br><br><br>");
}
catch(System.Data.SqlClient.SqlException e)
{
if(e.Number==2627)
{
RequiredFieldValidator2.ErrorMessage="此学号的同学已经存在,请重新输入!";
RequiredFieldValidator2.IsValid=false;
}
Response.Write(e.ToString());
}
}
</script>
</script>
<html>
<head>
<title>收费系统</title>
</head>
<body text="green">
<center>
<form runat="server">
<asp:datagrid id="stugrid" runat="server" />
<br/>
<table width="780" align="center" bordercolor="brown" bgcolor="#CACACA">
<tr>
<td valign="top" align="center">
<font size="4" color="green"><font face="宋体">请输入本次水、煤气数据</font><font size="4">
<font color="red" >*</font></font><font class="font9ptblack" face="宋体" color="green"><font size="4">表示必填
<br/></font></font>
</td>
</tr>
</table>
<table width="780" align="center" bordercolor="brown" bgcolor="#CACACA">
<tr >
<td valign="top" >
<font color="red">*</font><font color="green"><font face="宋体">用户名:</font>:</font>
<asp:TextBox id="name" columns="20" maxlength="50" runat="server"/>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
ControlToValidate="name" Display="Dynamic"
ErrorMessage="请输入正确的学生姓名!" runat="server"/>
</td>
<td valign="top" >
<font color="red">*</font><font color="green"><font face="宋体">用户ID号</font>:</font>
<asp:TextBox id="user_id" columns="20" maxlength="50" runat="server"/>
<asp:RequiredFieldValidator id="RequiredFieldValidator2"
ControlToValidate="user_id" Display="Dynamic"
ErrorMessage="请输入正确的学生学号!" runat="server"/>
</td>
</tr><br/>
<tr border="1">
<td valign="top" >
<font color="red">*</font><font color="green"><font face="宋体">用户住址</font>:</font>
<asp:TextBox id="adress" columns="20" maxlength="50" runat="server"/>
</td>
<td valign="top" >
<font color="red">*</font><font color="green"><font face="宋体">缴费时间</font>:</font>
<asp:TextBox id="time" columns="20" maxlength="50" runat="server"/>
</td>
</tr>
<tr>
<td valign="top" >
<font color="red">*</font><font color="green"><font face="宋体">上次水表数据</font>:</font>
<asp:TextBox id="pre_water" columns="20" maxlength="50" runat="server"/>
</td>
<td valign="top" >
<font color="red">*</font><font color="green"><font face="宋体">本次水表数据</font>:</font>
<asp:TextBox id="c_water" columns="20" maxlength="50" runat="server"/>
</td>
</tr><br/>
<tr>
<td valign="top" >
<font color="red">*</font><font color="green"><font face="宋体">上次煤气表数据</font>:</font>
<asp:TextBox id="pre_air" columns="20" maxlength="50" runat="server"/>
</td>
<td valign="top" >
<font color="red">*</font><font color="green"><font face="宋体">本次煤气表数据</font>:</font>
<asp:TextBox id="c_air" columns="20" maxlength="50" runat="server"/>
</td>
</tr><br/>
<tr>
<td valign="top" >
<font color="red">*</font><font color="green"><font face="宋体">水费价格</font>:</font>
<asp:TextBox id="p_water" columns="20" maxlength="50" runat="server"/>
</td>
<td valign="top" >
<font color="red">*</font><font color="green"><font face="宋体">煤气费价格</font>:</font>
<asp:TextBox id="p_air" columns="20" maxlength="50" runat="server"/>
</td>
</tr>
</table><br/>
<asp:Button id="Button1" Text="提交" OnClick="Button1_click" runat="server"
/>
<input type="reset" value="重填" >
</form>
</center>
</body>
</html>
它出错:
System.Data.SqlClient.SqlException: 变量名 '@c_air' 已声明。变量名在批查询或存储过程内部必须唯一。必须声明变量 '@p_air'。 在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) 在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 ASP.charge_aspx.Button1_click(Object sender, EventArgs E) 位置 e:\datasys\charge.aspx:行号 60
user_id name adress p_water p_air pre_water pre_air c_water c_air water_money air_money total time
1 小陈 西南大学 1 2 0 0 2 2 2 4 6 2007-10-1 0:00:00
5002 小梅 西南大学 1 3 2 2 6 9 4 12 16 2007-5-8 0:00:00
请输入本次水、煤气数据 *表示必填
*用户名:: 请输入正确的学生姓名! *用户ID号: 请输入正确的学生学号!
*用户住址: *缴费时间:
*上次水表数据: *本次水表数据:
*上次煤气表数据: *本次煤气表数据:
*水费价格: *煤气费价格: