我用了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();
解决方案 »
- 能否获取dataset中某个表的某一元素的值(如第一行第一列的值)
- Developer Express .NET v7.2 XtraReport打印页显示带有如图东西如何去掉
- 各位老大!有誰能告訴我,一個標準的軟件公司的開發流程嗎?詳細描述,因爲我想跳槽。
- 在线等!!定时检测程序是否无响应,如果无响应则重新启动该程序。如何实现???
- 关于使用UdpClient另开线程传文件,同一端口使用多次的问题
- DATATABLE中数据删除问题
- vs中c#做网页imageButton按钮的HotSpots属性设置
- 关于 smtp服务器的问题,请高手指教!!!
- 如何在DataGrid控件中,的每行数据后,加一个Button?
- C#中的自动属性问题
- 数组的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();