942,967 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 4085
  • ASP.NET RSS
Jan 26th, 2009
0

System.Data.Sqlclient.Sqlexception: Line1 incorrect syntax at ']'.

Expand Post »
Hi there
I'm a newbie to asp.net and trying to learning it doing small forum application. In the application I've a page which lists the available threads in a given topic and I used a gridview. In the gridview the thread's title is hyperlinked and when clicked it will redirect to another page that lists the thread's original message and any other replays to it.

My problem is it won't work as intended. When I click the thread's title it displays the page but with error message. Here is a portion of the browser's output


Server Error in '/' Application.

Line 1: 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: Line 1: Incorrect syntax near ']'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SqlException (0x80131904): Line 1: Incorrect syntax near ']'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +859322
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +736198
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188

here is the code for handling the click event of the selected index in the gridview
asp.net Syntax (Toggle Plain Text)
  1. protected void GridView1_SelectedIndexChanged( object sender, ventArgs e)
  2. {
  3. string threadID = GridView1.SelectedValue.ToString();
  4. string topicID = Request.QueryString[“topic”];
  5. Response.Redirect(“Messages.aspx?topic=” + topicID
  6. + “&thread=” + threadID);
  7. }


asp.net Syntax (Toggle Plain Text)
  1. <%@ Page Language="C#" MasterPageFile="~/MasterPage.master"
  2. AutoEventWireup="true" CodeFile="Messages.aspx.cs" Inherits="Messages" Title="Messages" %>
  3. <asp:Content ID="Content1" Runat="Server" ContentPlaceHolderID="ContentPlaceHolder1" >
  4. <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1">
  5. <ItemTemplate>
  6. <h3>Topic:<asp:Label ID="topicNameLabel" runat="server" Text='<%# Bind("name") %>' /></h3>
  7. </ItemTemplate>
  8. </asp:FormView>
  9. <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  10. ConnectionString ="<%$ ConnectionStrings:ForumConnectionString%>"
  11. SelectCommand="SELECT [name], [description] FROM [Topics] WHERE ([topicid] = @topicid)">
  12. <SelectParameters>
  13. <asp:QueryStringParameter Name="topicid" QueryStringField="topic" Type="Int32" />
  14. </SelectParameters>
  15. </asp:SqlDataSource>
  16. <asp:FormView ID="FormView2" runat="server" DataSourceID="SqlDataSource2">
  17. <ItemTemplate>
  18. <h3>Thread:<asp:Label ID="threadNameLabel" runat="server" Text='<%# Bind("subject") %>' /></h3>
  19. </ItemTemplate>
  20. </asp:FormView>
  21. <asp:SqlDataSource ID="SqlDataSource2" runat="server"
  22. ConnectionString="<%$ ConnectionStrings:ForumConnectionString%>"
  23. SelectCommand="SELECT [subject]FROM [Threads]WHERE ([threadid] = @threadid)">
  24. <SelectParameters>
  25. <asp:QueryStringParameter Name="threadid" QueryStringField="thread" Type="Int32" />
  26. </SelectParameters>
  27. </asp:SqlDataSource>
  28. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
  29. DataSourceID="SqlDataSource3" ShowHeader="False" AllowPaging="True" >
  30. <Columns>
  31. <asp:TemplateField>
  32. <ItemTemplate>
  33. At<asp:Label ID="Label1" runat="server" Text='<% #Eval("date") %>' />
  34. <asp:Label ID="Label2" runat="server" Text='<% #Eval("author") %>' />
  35. wrote:
  36. <br /><br />
  37. <table border=0>
  38. <tr><td width=300>
  39. <asp:Label ID="Label3" runat="server" Text='<% #Eval("message") %>' />
  40. </td></tr>
  41. </table>
  42. </ItemTemplate>
  43. </asp:TemplateField>
  44. </Columns>
  45. </asp:GridView>
  46. <asp:SqlDataSource ID="SqlDataSource3" runat="server"
  47. ConnectionString="<%$ ConnectionStrings:ForumConnectionString%>"
  48. SelectCommand="SELECT [author], [date], [message]FROM [Messages]WHERE ([threadid] = @threadid)ORDER BY [date]">
  49. <SelectParameters>
  50. <asp:QueryStringParameter Name="threadid" QueryStringField="thread" Type="Int32" />
  51. </SelectParameters>
  52. </asp:SqlDataSource>
  53. <br />
  54. <asp:Button ID="btnAdd" runat="server" Text="Add a Reply to this Thread" OnClick="btnAdd_Click" />
  55. <br /><br />
  56. <asp:LinkButton ID="btnReturn" runat="server" Text="Return to Threads page" OnClick="btnReturn_Click" />
  57. </asp:Content>

I know it is very tedious to read the above text but i don't have any choice

please help me


thank you very much
Last edited by peter_budo; Jan 27th, 2009 at 7:32 am. Reason: Correcting code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chombe is offline Offline
5 posts
since Nov 2008
Jan 26th, 2009
0

Re: System.Data.Sqlclient.Sqlexception: Line1 incorrect syntax at ']'.

you are right it is tedious to read through it!

my suggestions, the sql error appears to be a problem with the syntax of the inline queries you have specified for your SqlDataSources, more specifically something near a ']' you don't actually need the [] around column names unless the column names you have chosen match some SQL keyword or have spaces so get rid of them.

Next you don't need the () around the items in the where clause so remove them.

There appears to be spaces missing after the ] in some of the queries you should always have spaces after each element of a query.

2 approcahes to debugging this, one would be to copy the queries into Sql Management studio and try querying the database with them (replace the @parameter with a value which should work)
This will check that your queries actually work (this is the danger of inline sql no checking before you run it)

2nd approach remove all the gridviews and sqldatasources but one and check to see if the page works, do this for each one to find the problem query.

I think fixing the syntax as suggested above should fix the problems
Reputation Points: 68
Solved Threads: 9
Junior Poster
sedgey is offline Offline
130 posts
since Jan 2007
Jan 26th, 2009
0

Re: System.Data.Sqlclient.Sqlexception: Line1 incorrect syntax at ']'.

Thank you very much for helping me.
I spent two days trying to fix it.

keep on helping

God bless you!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chombe is offline Offline
5 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: connection string issue
Next Thread in ASP.NET Forum Timeline: aspnet_compiler / IIS metabase path error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC