更新时间:2023-11-08 09:12:40
在查询中添加空格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 querystring 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. )