本文介绍了调用Asp异常:'='附近的语法不正确。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在尝试进行注册,其中只要选择一个课程(下拉列表),就会自动显示所选课程的可用批次,但它会不断抛出此异常:'='附近的语法不正确。谢谢。 代码:
使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.Web.UI; 使用 System.Web.UI.WebControls; 使用 System.Data.SqlClient; 使用 System.Configuration; 使用 System.Data.SqlTypes; 使用 System.Data; 命名空间 WebApplication2 { public partial class _4:System.Web.UI.Page { string cs = ConfigurationManager.ConnectionStrings [ DBCS]。的ConnectionString; SqlConnection con; string cmd; SqlDataReader rdr; 受保护 void Page_Load( object sender,EventArgs e) { cmd = 从批处理中选择bat_id,start_d; ; } 受保护 void Submit_Click( object sender,EventArgs e) { string name = Name.Text; string pid = PID.Text; string pass = Pass.Text; DateTime dob = DateTime.Parse(DOB.Text); string gender = Gender.SelectedValue; string course = CourseList.SelectedValue; string email = Email.Text; } 受保护 void CourseList_SelectedIndexChanged( object sender,EventArgs e) { string 其中 = CourseList.SelectedValue.ToString(); string InitQuerry = 选择bat_id作为值,Year(start_d)为'Start',YEAR(start_d)+ duration为'End' + FROM Batch,course + WHERE c_name = @cname ; string cs = ConfigurationManager.ConnectionStrings [ DBCS]的ConnectionString。 使用(SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand(InitQuerry,con); cmd.Parameters.Add( new SqlParameter( @cname,SqlDbType.VarChar, 10 )); cmd.Parameters [ @ cname]。Value = 其中; (错误 此行。) con.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { string BatID =( String )(rdr.GetSqlValue( 0 )); string duration = rdr.GetSqlValue( 1 )+ - + rdr.GetSqlValue( 2 ); Batch.Items.Add( new ListItem(BatID,duration)); } con.Close(); } } } }我尝试了什么: 我现在不知道该怎么办,因为我无法弄清楚是什么原因异常。
解决方案在查询中添加空格 string InitQuerry =SELECT bat_id as Value,Year (start_d)为'Start',YEAR(start_d)+ duration为'End'+ FROM Batch,course+ WHERE c_name = @cname;
cmd.Parameters [ @ cname]。Value = where ; (错误 此行。)
在哪里? 关键字 [ ^ ] ...
Hello,I am trying to make a registration from in which as soon as select a course(drop downlist) the batches available for the chosen course are displayed automatically but it keeps throwing this exception : Incorrect syntax near '='. Thanks. Code:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Configuration; using System.Data.SqlTypes; using System.Data; namespace WebApplication2 { public partial class _4 : System.Web.UI.Page { string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; SqlConnection con; string cmd; SqlDataReader rdr; protected void Page_Load(object sender, EventArgs e) { cmd = "select bat_id,start_d from Batch; "; } protected void Submit_Click(object sender, EventArgs e) { string name = Name.Text; string pid = PID.Text; string pass = Pass.Text; DateTime dob = DateTime.Parse(DOB.Text); string gender = Gender.SelectedValue; string course = CourseList.SelectedValue; string email = Email.Text; } protected void CourseList_SelectedIndexChanged(object sender, EventArgs e) { string where = CourseList.SelectedValue.ToString(); string InitQuerry = "SELECT bat_id as Value, Year(start_d) as 'Start' , YEAR(start_d)+duration as 'End'" + "FROM Batch, course" + "WHERE c_name = @cname"; string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand(InitQuerry, con); cmd.Parameters.Add(new SqlParameter("@cname", SqlDbType.VarChar, 10)); cmd.Parameters["@cname"].Value = where; (Error is in this line. ) con.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { string BatID = (String)(rdr.GetSqlValue(0)); string duration = rdr.GetSqlValue(1) + "-" + rdr.GetSqlValue(2); Batch.Items.Add(new ListItem(BatID, duration)); } con.Close(); } } } }What I have tried: I don't know what to do now as i am not able to figure out what is the reason for the exception.
解决方案 add space in the query string InitQuerry = "SELECT bat_id as Value, Year(start_d) as 'Start' , YEAR(start_d)+duration as 'End'" + " FROM Batch, course" + " WHERE c_name = @cname"; cmd.Parameters["@cname"].Value = where; (Error is in this line. )What where is? A keyword[^]...
更多推荐
调用Asp.net异常:'='附近的语法不正确。
发布评论