0

I am a beginner that used to do VB.NET and now trying to learn C#. I've been trying to set up few forms where I can insert data into sql table, but every time I try to run the ASPX page it says

"There were errors building the project. Do you want to continue with the preview anyway?"

When I view the error list I get two:

  1. Warning 1: Generation of the designer file for Track.aspx failed: Error HRESULT E_FAIL has been returned from a call to a COM component.
  2. Error 2: Unrecognized escape sequence

The Error is pointing to the server part of the connection which is this:

Data Source=iskra-laptop\SQLEXPRESS; (the S is highlighted with red)

I am not sure on how to fix this, any help is appreciated. Here are both codes, the form and the C# files,

p.s.: I am aware of SqlParameter, and I will use that once I fix this issue :)


UPDATE: I fixed the connection where I was missing an extra \, but the first error is still there. Now, my variables are not being recognized, it says it does not exist in the current context, and Yes, I double checked the id names, they are correct.

keenthinker
  • 7,645
  • 2
  • 35
  • 45
user1830833
  • 151
  • 1
  • 2
  • 10
  • The first point is not an error but a warning (vs could build and run the project with warnings - it is a VS settings). Do you have resharper as in this [SO post](http://stackoverflow.com/questions/21319637/generation-of-the-designer-file-for-xxxxx-failed-error-hresult-e-fail-has-been)? – keenthinker Feb 01 '14 at 22:58
  • Well, after the SQL fix, all the variables are now errored. Error 1 The name 'regFoodNameTxt' does not exist in the current context same applies for the rest of the variables that are assigned to the text of the text boxes. – user1830833 Feb 01 '14 at 23:09
  • You need to cast the TextBox.Text to an int! Use [int.TryParse](http://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx) - int cals; int.TryParse(regFoodCalTxt.Text, out cals); You get errors, because it is not possible to put a text to an int directly, which int cals = TextBoxControl.Text tries to do. – keenthinker Feb 01 '14 at 23:23
  • I tried these 2 ways with same result, still does not exist in the current context, the text box's name that is; int i = int.Parse(regFoodCalTxt.Text); int i = Convert.ToInt32(regFoodCalTxt.Text); – user1830833 Feb 01 '14 at 23:39
  • I don't think this is answerable without more information. What file names do the c# code and the form have? Is the uploaded form the entirety of the file? –  Feb 02 '14 at 21:13
  • I actually fixed all problems and everything works now. For some reason, it did not like the names I had like regFoodCalTxt or regFoodNameTxt as id for the textbox, but after I renamed it to registerFoodCalTxt it was recognized in the c# code, so I changed all reg to register. Weird, I don't think that is a reserved name for anything, but oh well – user1830833 Feb 02 '14 at 21:24

1 Answers1

3
SqlConnection myConnection = new SqlConnection("Data Source=iskra-laptop\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;Pooling=False");

Add a @ in front of the string:

SqlConnection myConnection = new SqlConnection(@"Data Source=iskra-laptop\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;Pooling=False");

\ forms an escape sequence in strings. For example \n is a newline. \S isn't a valid escape sequence, so the compiler is crying.

The @ tells the compiler to treat the string literally. In other words, ignore the standard escape sequences.

Reading material on what the @ is: http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-does-an-before-the-start-of-a-string-literal-mean.aspx

Another solution is the following:

SqlConnection myConnection = new SqlConnection("Data Source=iskra-laptop\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;Pooling=False");

Now we're escaping the slash, so \\S is interpreted as \S, as you intended.

  • Thx. I actually saw that I was missing an extra \ 2 min after I posted it here, however, that only solved the 2nd error. The first still exists and I am not seeing the problem. – user1830833 Feb 01 '14 at 22:48
  • Well, after the SQL fix, all the variables are now errored. Error 1 The name 'regFoodNameTxt' does not exist in the current context same applies for the rest of the variables that are assigned to the text of the text boxes – user1830833 Feb 01 '14 at 23:10