(原)ASP.NET中的单引号和双引号
最近做的一个仓库管理(OA)系统遇到的问题
我需要使用下拉菜单读取数据库整条的记录,进行运算,代码如下.但是运行老是出错.找了一天时间才找出错误,少了一对单引号
<%@ Page Language="C#" AutoEventWireup="true" EnableSessionState="True" CodeFile="test.aspx.cs" Inherits="Web_test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="info11" runat="server" Text="Label"></asp:Label>
<asp:DropDownList ID="L_GoodsName" runat="server"
DataTextField="info2" DataValueField="info_id" Width="197px" AutoPostBack="True" OnSelectedIndexChanged="L_GoodsName_SelectedIndexChanged" DataSourceID="SqlDataSource1" AppendDataBoundItems="true">
<asp:ListItem Value="" Selected="True">==请选择==</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"></asp:SqlDataSource>
</form>
</body>
</html>
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 Web_test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{ 
SqlDataSource1.SelectCommand = "SELECT * FROM [r_ware] ORDER BY [info_id] DESC"; //选择商品菜单
string num = GetNum();
this.info11.Text = num;
GetNum();
//}
//==================
//存放信息
//Application["UserNameID"]="1000";
//读取信息
//String TextBox1= Application["UserNameID"].ToString();
}
protected void L_GoodsName_SelectedIndexChanged(object sender, EventArgs e)
{
string info_id = L_GoodsName.SelectedValue;//获得此商品的ID 

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DBConnectionString"]);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from r_ware where info_id = " + info_id + " ", conn);
SqlDataReader my = cmd.ExecuteReader();
if (my.Read())
{
TextBox1.Text = my["info7"].ToString();
TextBox2.Text = my["info10"].ToString();
// L_GoodsSum.Text = my["info"].ToString();
}
my.Close();
//Response.Write(((DropDownList)sender).SelectedItem);
}
public String GetNum()
{
Application["id"] = L_GoodsName.SelectedValue;
string id = Application["id"].ToString();
String Result = null;
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DBConnectionString"]);
conn.Open();
SqlCommand cmd = new SqlCommand("select info7*info10 from r_ware where info_id = " + id + " ", conn);
SqlDataReader my = cmd.ExecuteReader();
if (my.HasRows)
{
if (my.Read())
{
Result = my[0].ToString();
}
}
conn.Close();
return Result;
}

protected void Button1_Click(object sender, EventArgs e)
{
}
}
改.CS中的代码
"select info7*info10 from r_ware where info_id = '" + id + "' "
这里需要多一对单引号。。。到处测试成功 之前请教了网上好多人和公司的同事都没找到解决 看来今天又多学了东西 回去再看看书本吧 ^0^


浙公网安备 33010602011771号