473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

7
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

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Configuration;
  9. using System.Web.Security;
  10. using System.Web.UI;
  11. using System.Web.UI.HtmlControls;
  12. using System.Web.UI.WebControls;
  13. using System.Web.UI.WebControls.WebParts;
  14. using System.Xml.Linq;
  15. using System.Text;
  16.  
  17. public partial class retrieverecord : System.Web.UI.Page
  18. {
  19.     private string connString = WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
  20.  
  21.     protected void Page_Load(object sender, EventArgs e)
  22.     {
  23.         if (!this.IsPostBack)
  24.         {
  25.             FillAuthor();
  26.         }
  27.     }
  28.     private void FillAuthor()
  29.     {
  30.         ddList.Items.Clear();
  31.  
  32.      //Define the select statement, 3 pieces of info 
  33.      //namely the unique id and first and last name of suthor
  34.  
  35.         string selectSQL = "SELECT au_lname, au_fname, au_id FROM Authors";
  36.  
  37.     // Define the ADO objects
  38.         SqlConnection cn = new SqlConnection(connString);
  39.         SqlCommand cmd = new SqlCommand(selectSQL, cn);
  40.         SqlDataReader reader;
  41.  
  42.  
  43.     //try to open database and read info.
  44.  
  45.         try
  46.         {
  47.             cn.Open();
  48.             reader = cmd.ExecuteReader();
  49.             while (reader.Read())
  50.             {
  51.  
  52.                 ListItem newItem = new ListItem();
  53.                 newItem.Text = reader["au_lname"] + ", " + reader["au_fname"];
  54.                 newItem.Value = reader["au_id"].ToString();
  55.                 ddList.Items.Add(newItem);
  56.             }
  57.             reader.Close();
  58.         }
  59.         catch (Exception err)
  60.         {
  61.             lblResult.Text = "Error reading list of names.";
  62.             lblResult.Text += err.Message;
  63.         }
  64.         finally
  65.         {
  66.             cn.Close();
  67.         }
  68.     }
  69.  
  70.     // Every time index is changed data will be reloaded.
  71.     protected void ddList_SelectedIndexChanged(object sender, EventArgs e)
  72.     {
  73.     //Create a Select statement that searches for a record
  74.     //Matching the specific author ID from the Value property
  75.  
  76.         string SQLselect;
  77.         SQLselect = "SELECT * FROM authors";
  78.         SQLselect += "WHERE au_id='"+ddList.SelectedItem.Value+"'";
  79.  
  80.      //Define the ADO.NET objects
  81.  
  82.         SqlConnection con = new SqlConnection(connString);
  83.         SqlCommand cmd = new SqlCommand(SQLselect,con);
  84.         SqlDataReader reader;
  85.  
  86.      //try to open database and read information.
  87.         try
  88.         {
  89.             con.Open();
  90.             reader = cmd.ExecuteReader();
  91.             reader.Read();
  92.  
  93.         //Fill the Control
  94.  
  95.             txtBox1.Text = reader["au_ID"].ToString();
  96.             txtBox2.Text = reader["au_fname"].ToString();
  97.             txtBox3.Text = reader["au_lname"].ToString();
  98.             txtBox4.Text = reader["phone"].ToString();
  99.             txtBox5.Text = reader["address"].ToString();
  100.             txtBox6.Text = reader["city"].ToString();
  101.             txtBox7.Text = reader["state"].ToString();
  102.             txtBox8.Text = reader["zip"].ToString();
  103.  
  104.             chkContract.Checked = (bool)reader["contract"];
  105.  
  106.             lblResult.Text = "";
  107.  
  108.             reader.Close();
  109.         }
  110.         catch (Exception err)
  111.         {
  112.             lblResult.Text = "Error getting author.<br />";
  113.             lblResult.Text += err.Message;
  114.         }
  115.         finally
  116.         {
  117.             con.Close();
  118.         }
  119.  
  120.  
  121.     }
  122. }
when I debug error is shown. Please take a look and help.
Mar 31 '11 #1
2 2784
VijaySofist
107 100+
Hi,

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

For Example
Expand|Select|Wrap|Line Numbers
  1. if ((ddList.DataSource != null)) {
  2. //Your Code Here
  3. }
If this does not solve your problem, take a screenshot of that error & give here

All the Best

Regards
Vijay.R
Mar 31 '11 #2
silwar
7
Well i found the problem. it was select statement string
i.e.

thats how I created string

Expand|Select|Wrap|Line Numbers
  1. string selSQL;
  2.         selSQL = "SELECT * FROM authors";
  3.         selSQL += "WHERE au_id=@value";

This one works

Expand|Select|Wrap|Line Numbers
  1. 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
Mar 31 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Amit D.Shinde | last post by:
Hi Experts, i am writting a stored procedure in sql server 7. Its a simple stored procedure It is my first stored procedure. I want insert a record in table if the primary key field user id...
11
by: Mark Findlay | last post by:
Hello Experts! I am attempting to use the OleDbCommand.ExecuteScaler() function within my ASP.NET C# web page to perform a simple validation, but receive the following error: "Incorrect...
4
by: Larry Woods | last post by:
I have a Messagebox that looks like: MessageBox.Show("There are pending changes for this patient. Do you want to continue to close?", "Pending Changes", MessageBoxButtons.YesNo,...
1
by: Sandesh | last post by:
Hello All, Me saying " has any body come across such error would be underestimating". Well I am getting a very peculiar and unique error "Line 1: Incorrect syntax near 'Actions'." ...
1
by: Dax | last post by:
When i insert some values float in a form made with ASP appear an error message like this: Microsoft OLE DB Provider for SQL Server (0x80040E14) Line 1: Incorrect syntax near ','. sql="select...
1
by: mabubakarpk | last post by:
HI. I am using SQL Server 2000. I generate a script of some table from EmpDB database when I run script in query analyzer it return error "Incorrect syntax near 'COLLATE'." Scripts is ...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
1
by: karenkksh | last post by:
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. ...
2
by: Mark B | last post by:
When I click the 'Update' button on a ASP.Net 3.5 GridView record, I get an error: Incorrect syntax near 'nvarchar'. Everything else there works OK. Here is the code: <%@ Page...
1
by: Mia Archibald | last post by:
SELECT mcr2008.st, (SUM(mcr2008.bedsize)-SUM(mcr2007.bedsize))/SUM(mcr2007.bedsize) FROM ( ( SELECT mcrid, st , SUM(bedsize) as bedsize FROM MCRCR_96_to_08 WHERE...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.