My query is perfect (I have verified it in SQL Server Management Studio). My code is perfect, still I am getting this syntax error:
Incorrect syntax near '='. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.
public partial class Temporaryche : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ddlTDept.Items.Clear();
ddlTBranch.Items.Clear();
string connectionString = GlobalVariables.databasePath;
SqlConnection sqlCon = new SqlConnection(connectionString);
string query = "select fac.fac_name, dp.dp_name, br.br_name from STUDENT s, DIVISON dv, BRANCH br, DEPT dp, FACULTY fac, CLASS cls, DEGREE dg where dg.dg_id = cls.dg_id and cls.cls_id = s.cls_id and fac.fac_id = dp.fac_id and dp.dp_id = br.dp_id and br.br_id = dv.br_id and s.dv_id = dv.dv_id and s.prn_no = " + txtSearch.Text;
sqlCon.Open();
SqlCommand cmd = new SqlCommand(query, sqlCon);
SqlDataReader reader = cmd.ExecuteReader();
string facultyName = reader.GetValue(0).ToString();
string deptName = reader.GetValue(1).ToString();
string branchName = reader.GetValue(2).ToString();
ddlTFaculty.SelectedValue = facultyName;
query = "select dp_name from DEPT where fac_id=(select fac_id where fac_name='" + facultyName + "')";
cmd = new SqlCommand(query, sqlCon);
reader = cmd.ExecuteReader();
ddlTDept.Items.Clear();
while (reader.Read())
{
ddlTDept.Items.Add(reader.GetValue(0).ToString());
}
ddlTDept.SelectedValue = deptName;
sqlCon.Close();
}
}