Drop down list index changing displaying error Incorrect syntax near '='

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • silwar
    New Member
    • Mar 2011
    • 7

    Drop down list index changing displaying error Incorrect syntax near '='

    Hi,

    I have already stored value of author id in list item value while page loads. but when I change index of drop down list to view author information error is shown. I tried to debug using check points and it seems Sqlreader isn't reading database second time.

    Incorrect syntax near '='..

    Here is my code

    Code:
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.Configuration;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Text;
    
    public partial class retrieverecord : System.Web.UI.Page
    {
        private string connString = WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                FillAuthor();
            }
        }
        private void FillAuthor()
        {
            ddList.Items.Clear();
    
         //Define the select statement, 3 pieces of info 
         //namely the unique id and first and last name of suthor
    
            string selectSQL = "SELECT au_lname, au_fname, au_id FROM Authors";
    
        // Define the ADO objects
            SqlConnection cn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(selectSQL, cn);
            SqlDataReader reader;
    
    
        //try to open database and read info.
    
            try
            {
                cn.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
    
                    ListItem newItem = new ListItem();
                    newItem.Text = reader["au_lname"] + ", " + reader["au_fname"];
                    newItem.Value = reader["au_id"].ToString();
                    ddList.Items.Add(newItem);
                }
                reader.Close();
            }
            catch (Exception err)
            {
                lblResult.Text = "Error reading list of names.";
                lblResult.Text += err.Message;
            }
            finally
            {
                cn.Close();
            }
        }
    
        // Every time index is changed data will be reloaded.
        protected void ddList_SelectedIndexChanged(object sender, EventArgs e)
        {
        //Create a Select statement that searches for a record
        //Matching the specific author ID from the Value property
    
            string SQLselect;
            SQLselect = "SELECT * FROM authors";
            SQLselect += "WHERE au_id='"+ddList.SelectedItem.Value+"'";
    
         //Define the ADO.NET objects
    
            SqlConnection con = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(SQLselect,con);
            SqlDataReader reader;
    
         //try to open database and read information.
            try
            {
                con.Open();
                reader = cmd.ExecuteReader();
                reader.Read();
    
            //Fill the Control
    
                txtBox1.Text = reader["au_ID"].ToString();
                txtBox2.Text = reader["au_fname"].ToString();
                txtBox3.Text = reader["au_lname"].ToString();
                txtBox4.Text = reader["phone"].ToString();
                txtBox5.Text = reader["address"].ToString();
                txtBox6.Text = reader["city"].ToString();
                txtBox7.Text = reader["state"].ToString();
                txtBox8.Text = reader["zip"].ToString();
                
                chkContract.Checked = (bool)reader["contract"];
    
                lblResult.Text = "";
    
                reader.Close();
            }
            catch (Exception err)
            {
                lblResult.Text = "Error getting author.<br />";
                lblResult.Text += err.Message;
            }
            finally
            {
                con.Close();
            }
    
    
        }
    }
    when I debug error is shown. Please take a look and help.
    Last edited by silwar; Mar 31 '11, 09:29 AM. Reason: Editing some code
  • VijaySofist
    New Member
    • Jun 2007
    • 107

    #2
    Hi,

    Check the DataSource using If Condition and Give all the SelectedIndex Code there

    For Example
    Code:
    if ((ddList.DataSource != null)) {
    //Your Code Here
    }
    If this does not solve your problem, take a screenshot of that error & give here

    All the Best

    Regards
    Vijay.R

    Comment

    • silwar
      New Member
      • Mar 2011
      • 7

      #3
      Well i found the problem. it was select statement string
      i.e.

      thats how I created string

      Code:
      string selSQL;
              selSQL = "SELECT * FROM authors";
              selSQL += "WHERE au_id=@value";

      This one works

      Code:
      selSQL = "SELECT * FROM authors WHERE au_id=@value";
      I think while declaring string for command object we can't append it with other string it need to be declared in a single line

      also i learned about parameters

      Thanks for your reply Vijay

      Comment

      Working...