CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    1

    Please help,error in c# code - beginner

    im trying to query and display a dictionary saved in a local sql databse.

    i get an error

    SqlDataReader dr = cmd.ExecuteReader(); - SqlException was unhandled - incorrect syntax near 'CHARINDEX'

    help much appricated



    view plaincopy to clipboardprint?
    01.using System;
    02.using System.Collections.Generic;
    03.using System.ComponentModel;
    04.using System.Data;
    05.using System.Drawing;
    06.using System.Linq;
    07.using System.Text;
    08.using System.Windows.Forms;
    09.using System.Text.RegularExpressions;
    10.using System.Collections;
    11.using System.Data.Sql;
    12.using System.Data.SqlClient;
    13.using System.Data.SqlTypes;
    14.using System.Data.Common;
    15.using System.Web.Configuration;
    16.using System.Configuration;
    17.
    18.namespace JD
    19.{
    20. public partial class Crabble : Form
    21. {
    22. string Letters;
    23.
    24. public Crabble()
    25. {
    26. InitializeComponent();
    27. }
    28.
    29. private void Crabble_Load(object sender, EventArgs e)
    30. {
    31. textCrabble.Focus();
    32. }
    33.
    34. private void buttonClose_Click(object sender, EventArgs e)
    35. {
    36. this.Hide();
    37. }
    38.
    39. private void buttonCrabble_Click(object sender, EventArgs e)
    40. {
    41. Letters = textCrabble.Text.ToUpper();
    42. bool valid = true;
    43. bool print = true;
    44. Regex re = new Regex(@"[^A-Za-z\*]");
    45. StringBuilder sb = new StringBuilder();
    46.
    47. if (re.IsMatch(Letters))
    48. {
    49. valid = false;
    50. MessageBox.Show("Letters and Asterisks only please.");
    51. }
    52.
    53. if (valid)
    54. {
    55. string Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    56. ArrayList alphabet = new ArrayList();
    57.
    58. for (int i = 0; i < Alpha.Length; i++)
    59. {
    60. alphabet.Add(Alpha[i]);
    61. }
    62.
    63. int len = Letters.Length;
    64. bool blank = false;
    65. if (Letters.IndexOf("*") >= 0)
    66. blank = true;
    67. string querystart = "SELECT CrabbleDictionary FROM CrabbleJohn WHERE";
    68. string querymid = "";
    69. string queryend = @"AND CrabbleLength <= " + len.ToString() +
    70. " ORDER BY CrabbleLength, CrabbleDictionary";
    71.
    72. if (!blank)
    73. {
    74. foreach (char letter in Letters)
    75. {
    76. alphabet.Remove(letter);
    77. }
    78. foreach (char remainingletter in alphabet)
    79. {
    80. querymid += @"And Not CHARINDEX('" + remainingletter.ToString()
    81. + "',[CrabbleDictionary])>0 ";
    82. }
    83. querymid = querymid.Substring(4);
    84. }
    85. else
    86. {
    87. queryend = queryend.Substring(4);
    88. }
    89. string query = querystart + querymid + queryend;
    90. string connect = ConfigurationManager.ConnectionStrings
    91. ["CrabbleJohn"].ConnectionString;
    92. using (SqlConnection conn = new SqlConnection(connect))
    93. {
    94. SqlCommand cmd = new SqlCommand(query, conn);
    95. conn.Open();
    96. SqlDataReader dr = cmd.ExecuteReader();
    97. if (dr.HasRows)
    98. {
    99. while (dr.Read())
    100. {
    101. int pos;
    102. string totest = Letters;
    103. string word = dr[0].ToString();
    104. for (int i = 0; i < word.Length; i++)
    105. {
    106. pos = totest.IndexOf(word[i]);
    107. if (pos >= 0)
    108. {
    109. totest = totest.Remove(pos, 1);
    110. print = true;
    111. }
    112. else
    113. {
    114. if (!blank)
    115. {
    116. print = false;
    117. break;
    118. }
    119. else
    120. {
    121. pos = totest.IndexOf("*");
    122. if (pos >= 0)
    123. {
    124. totest = totest.Remove(pos, 1);
    125. print = true;
    126. }
    127. else
    128. {
    129. print = false;
    130. break;
    131. }
    132. }
    133. }
    134. }
    135. if (print)
    136. {
    137. sb.Append(word + "<br />");
    138. }
    139. }
    140. }
    141. dr.Close();
    142. textResults.Text = sb.ToString();
    143. }
    144. }
    145. }
    146. }
    147.}

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Please help,error in c# code - beginner

    Quote Originally Posted by johnpdavidson View Post
    help much appricated
    remove the line numbers and add the code tags... perhaps then I will read it. it's not even possible to copy it to visual studio.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Please help,error in c# code - beginner

    i get an error

    SqlDataReader dr = cmd.ExecuteReader(); - SqlException was unhandled - incorrect syntax near 'CHARINDEX'

    help much appricated
    Code:
    querymid += @"And Not CHARINDEX('" + remainingletter.ToString() 
    + "',[CrabbleDictionary])>0 ";
    The error tells you EXACTLY where the problem is. It looks like some sort of escape sequence issue. Mess around with it on your own and see what ya come up with.

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Please help,error in c# code - beginner

    have you tested this query in the sql management studio? does it work?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured