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
Expand|Select|Wrap|Line Numbers
- 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();
- }
- }
- }