我用了4三个dropdownlist控件从数据库里提出了一些数据.
现在想通过选择dropdownlist中的项,得到当前选的值
然后再将得到的值插入另一表里现提示如下错误:
System.Data.SqlClient.SqlException: 在此上下文中不允许使用 'MonitorValue'。此处只允许使用常量、表达式或变量。不允许使用列名。代码如下:
string TypeValue = TypeList.SelectedItem.Value;
string AccessValue = null;
string MonitorValue = null;
string AlermValue = null;
if (AccessList.SelectedIndex != -1)
{
AccessValue = AccessList.SelectedItem.Value;
}
if (MonitorList.SelectedIndex != -1)
{
MonitorValue = MonitorList.SelectedItem.Value;
}
if (AlermList.SelectedIndex != -1)
{
AlermValue = AlermList.SelectedItem.Value;
} System.Text.StringBuilder sql = new System.Text.StringBuilder();
sql.Append("insert into Device(Access_ID,MonitorPolicyID,AlarmPolicyID,Device_Name,");
sql.Append("Device_Code,DeviceType_ID,Device_Address,Device_Longitude,Device_Latitude,");
sql.Append("Device_BuyTime,Device_BuyOrder,Device_Logpath,Device_Note)");
sql.Append("values (" + "AccessValue,MonitorValue,AlermValue,E_DevName.Text");
sql.Append("E_code.Text,TypeValue,E_DevAdder.Text,E_longitude.Text,E_latitude.Text,");
sql.Append("E_code.Text,Typevalue,E_DevAdder.Text,E_longitude.Text,E_latitude.Text,");
sql.Append("+E_BuyTime.Text,E_BuyOrder.Text,E_Logpath.Text,E_Note.Text)"); // System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('"+MonitorList.SelectedItem.Value+"');</script>"); SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]);
conn.Open();
System.Data.SqlClient.SqlCommand comm = new SqlCommand(sql.ToString(),conn); //sql
comm.ExecuteNonQuery();
现在想通过选择dropdownlist中的项,得到当前选的值
然后再将得到的值插入另一表里现提示如下错误:
System.Data.SqlClient.SqlException: 在此上下文中不允许使用 'MonitorValue'。此处只允许使用常量、表达式或变量。不允许使用列名。代码如下:
string TypeValue = TypeList.SelectedItem.Value;
string AccessValue = null;
string MonitorValue = null;
string AlermValue = null;
if (AccessList.SelectedIndex != -1)
{
AccessValue = AccessList.SelectedItem.Value;
}
if (MonitorList.SelectedIndex != -1)
{
MonitorValue = MonitorList.SelectedItem.Value;
}
if (AlermList.SelectedIndex != -1)
{
AlermValue = AlermList.SelectedItem.Value;
} System.Text.StringBuilder sql = new System.Text.StringBuilder();
sql.Append("insert into Device(Access_ID,MonitorPolicyID,AlarmPolicyID,Device_Name,");
sql.Append("Device_Code,DeviceType_ID,Device_Address,Device_Longitude,Device_Latitude,");
sql.Append("Device_BuyTime,Device_BuyOrder,Device_Logpath,Device_Note)");
sql.Append("values (" + "AccessValue,MonitorValue,AlermValue,E_DevName.Text");
sql.Append("E_code.Text,TypeValue,E_DevAdder.Text,E_longitude.Text,E_latitude.Text,");
sql.Append("E_code.Text,Typevalue,E_DevAdder.Text,E_longitude.Text,E_latitude.Text,");
sql.Append("+E_BuyTime.Text,E_BuyOrder.Text,E_Logpath.Text,E_Note.Text)"); // System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('"+MonitorList.SelectedItem.Value+"');</script>"); SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]);
conn.Open();
System.Data.SqlClient.SqlCommand comm = new SqlCommand(sql.ToString(),conn); //sql
comm.ExecuteNonQuery();
解决方案 »
- VS2005制作的水晶报表条码,39码可以扫描,128条码扫描不了,晕死
- 跪求 asp.net 长文章分页实现思路和代码
- 怎样新建一个可以debug的配置?
- 高手求助:【求一组节点的生成树】
- 求一个用mshtml写的例子
- C#的codebehind的变量如何传递给客户端的javascript代码中?
- 我用C#操作 word 模板(dot文件),模板文件是设计好的,其中有一些内容动态变化,我怎么用c#将动态内容写入模板文件?
- 谁有VISIO画UML的教程
- 这样的错误什么原因CS0673: System.Void cannot be used from C# -- use typeof(void) to get the void type object.
- c#操作word文档
- 数组的Sort方法的用法
- c#好书推荐:关于《c#设计模式》
System.Text.StringBuilder sql = new System.Text.StringBuilder();
sql.Append("insert into Device(Access_ID,MonitorPolicyID,AlarmPolicyID,Device_Name,");
sql.Append("Device_Code,DeviceType_ID,Device_Address,Device_Longitude,Device_Latitude,");
sql.Append("Device_BuyTime,Device_BuyOrder,Device_Logpath,Device_Note)");
sql.Append("values (" + AccessValue+","+"MonitorValue"+","+"AlermValue"+","+E_DevName.Text+",");
sql.Append(E_code.Text+","+TypeValue+","+E_DevAdder.Text+","+E_longitude.Text+","+E_latitude.Text+",");
sql.Append(E_code.Text+","+Typevalue+","+E_DevAdder.Text+","+E_longitude.Text+","+E_latitude.Text+",");
sql.Append(E_BuyTime.Text+","+E_BuyOrder.Text+","+E_Logpath.Text+","+E_Note.Text+")");
Response.Write(sql.ToString());或者设置断点看看SQL语句什么地方错误
你的sql语句好象还有问题
断下点看sql.ToString()的值是什么我一般用string.Format("insert into table(a,b) values({0},{1})",a,b)
这比较好调试 排bug
string AccessValue = null;
string MonitorValue = null;
string AlermValue = null;
if (AccessList.SelectedIndex != -1)
{
AccessValue = AccessList.SelectedItem.Value;
}
if (MonitorList.SelectedIndex != -1)
{
MonitorValue = MonitorList.SelectedItem.Value;
}
if (AlermList.SelectedIndex != -1)
{
AlermValue = AlermList.SelectedItem.Value;
}System.Text.StringBuilder sql = new System.Text.StringBuilder();
sql.Append("insert into Device(Access_ID,MonitorPolicyID,AlarmPolicyID,Device_Name,");
sql.Append("Device_Code,DeviceType_ID,Device_Address,Device_Longitude,Device_Latitude,");
sql.Append("Device_BuyTime,Device_BuyOrder,Device_Logpath,Device_Note)");
//**********************
sql.Append("values (" + "'"+AccessValue,MonitorValue+"','"+AlermValue,E_DevName.Text+"'");
sql.Append("'"+E_code.Text,TypeValue+"','"+E_DevAdder.Text+"','"+E_longitude.Text+"','"+E_latitude.Text+"',");
sql.Append("'"+E_code.Text,Typevalue+"','"+E_DevAdder.Text+"','"+E_longitude.Text+"','"+E_latitude.Text+"',");
sql.Append("'"+E_BuyTime.Text+"','"+E_BuyOrder.Text+"','"+E_Logpath.Text,E_Note.Text+"')");
//*************************************//System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('"+MonitorList.SelectedItem.Value+"');</script>");SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]);
conn.Open();
System.Data.SqlClient.SqlCommand comm = new SqlCommand(sql.ToString(),conn);//sql
comm.ExecuteNonQuery();
覆盖测试
Device_Code,DeviceType_ID,Device_Address,Device_Longitude,Device_Latitude,
Device_BuyTime,Device_BuyOrder,Device_Logpath,Device_Note)
values (125,MonitorValue,AlermValue,dsf,dsf,4,dsf, , ,dsf,4,dsf, , ,2005-6-3,dsf,dsf,)
MonitorList
AlermList 这个两dropdwonlist无值所至呢
sql.Append("insert into Device(Access_ID,MonitorPolicyID,AlarmPolicyID,Device_Name,");
sql.Append("Device_Code,DeviceType_ID,Device_Address,Device_Longitude,Device_Latitude,");
sql.Append("Device_BuyTime,Device_BuyOrder,Device_Logpath,Device_Note)");
sql.Append("values (" + AccessValue+","+MonitorValue+","+AlermValue+","+E_DevName.Text+",");
sql.Append(E_code.Text+","+TypeValue+","+E_DevAdder.Text+","+E_longitude.Text+","+E_latitude.Text+",");
sql.Append(E_code.Text+","+Typevalue+","+E_DevAdder.Text+","+E_longitude.Text+","+E_latitude.Text+",");
sql.Append(E_BuyTime.Text+","+E_BuyOrder.Text+","+E_Logpath.Text+","+E_Note.Text+")");
sql.Append("insert into Device( Access_ID,MonitorPolicyID,AlarmPolicyID,Device_Name,");
sql.Append("Device_Code,DeviceType_ID,Device_Address,Device_Longitude,");
sql.Append("Device_Latitude,Device_BuyTime,Device_BuyOrder, Device_Logpath, Device_Note)");
sql.Append(" values({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12})");
string strSQL = string.Format(sql.ToString(),AccessValue,MonitorValue,AlermValue,E_DevName.Text,
E_code.Text,TypeValue,E_DevAdder.Text,E_longitude.Text,E_latitude.Text,
E_BuyTime.Text,E_BuyOrder.Text,E_Logpath.Text,E_Note.Text);SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]);
conn.Open();
System.Data.SqlClient.SqlCommand comm = new SqlCommand(strSQL,conn); //sql
comm.ExecuteNonQuery();