0

I'm very new to C# so I'm using tutorials to get by.

I'm following a youtube tutorial on creating a login form. Linked here, at 10:00 mins into the video he starts inputting a connection string which was retrieved from the database earlier.

I followed the exact same steps inputting the following:

SqlConnection connnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C: \Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Database1.mdf";Integrated Security=True");

However, I was surprised to see 26 errors all to do with that line. I suspected it was something to do with the string, as I read here that

You must also escape the back backslashes in your connection string \ becomes this \\

However, by using the @ symbol before the string it turns into a verbatim string as I read here so that isn't the problem.

Now, I'm assuming that the issue lies with my app.config as in other posts I've seen them add their connection string there, and then create a new connection string referencing that, but in the listed tutorial he doesn't do this, which leaves me confused.

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

Login button

private void buttonLogin_Click(object sender, EventArgs e)
{
    SqlConnection connnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C: \Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Database1.mdf";Integrated Security=True");

    SqlDataAdapter sda = new SqlDataAdapter("select count(*) from login where Username ='" + textBoxUsername.Text + "' and Password='" + textBoxPassword.Text + "'", connnection);

    DataTable dt = new DataTable();
    sda.Fill(dt);

    if (dt.Rows[0][0].ToString() == "1")
    {
        LoginSuccess();
    }
    else
    {
        MessageBox.Show("Failed login.");
    }
}

Error messages

Error CS1003 Syntax error, ',' expected Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1009 Unrecognized escape sequence Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1056 Unexpected character '\' Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1003 Syntax error, ',' expected Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1056 Unexpected character '\' Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1003 Syntax error, ',' expected Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1056 Unexpected character '\' Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1003 Syntax error, ',' expected Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1056 Unexpected character '\' Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1003 Syntax error, ',' expected Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1003 Syntax error, ',' expected Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1012 Too many characters in character literal Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1009 Unrecognized escape sequence Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1003 Syntax error, ',' expected Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1056 Unexpected character '\' Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1003 Syntax error, ',' expected Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1003 Syntax error, ',' expected Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1739 The best overload for 'SqlConnection' does not have a parameter named 'C' Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS0103 The name 'Users' does not exist in the current context Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS0103 The name 'Kevin' does not exist in the current context Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS1738 Named argument specifications must appear after all fixed arguments have been specified. Please use language version 7.2 or greater to allow non-trailing named arguments. Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS0103 The name 'source' does not exist in the current context Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS0103 The name 'repos' does not exist in the current context Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS0103 The name 'Kops' does not exist in the current context Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS0103 The name 'Toolbox' does not exist in the current context Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active
Error CS0103 The name 'Database1' does not exist in the current context Kops' Toolbox C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Form1.cs 62 Active

Database, named Database1 table is called UserData

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
UnAlpha
  • 127
  • 14
  • [Can I escape a double quote in a verbatim string literal?](//stackoverflow.com/q/1928909) – 001 Apr 25 '18 at 03:42
  • try this : "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\Kevin\\source\\repos\\Kops\' Toolbox\\Kops\' Toolbox\\Database1.mdf\";Integrated Security=True"; – Gaurang Dave Apr 25 '18 at 03:43
  • I recommend you use ORM tools like entity framework – Mazaher Bazari Apr 25 '18 at 03:43
  • you have some major space after `c:` – T.S. Apr 25 '18 at 04:17
  • @TS The connections string is copied and pasted, that space was there. I also tried removing the space and running it to no avail. – UnAlpha Apr 25 '18 at 11:32
  • Look, you have some serious issues in your connection string. Try this `@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\Kevin\source\repos\Kops\Toolbox\KopsToolbox\Database1.mdf"";Integrated Security=True"`. I don't know if this part is correct -- `C:\Users\Kevin\source\repos\Kops\Toolbox\KopsToolbox\Database1.mdf`. If anything, get correct path and paste there. – T.S. Apr 25 '18 at 15:08

1 Answers1

1

You're nearly there. The main problem is that your string contains double-quotes; but a string itself is delimited by double-quotes, so you need to 'escape' that character (despite that the accepted answer linked question on verbatim strings doesn't mention that - I will add a comment, but the other answer mentions it).

You should be able to see that it's not right by the colouring of the source in your editor. If you can't, then get a better editor.

The way of escaping this varies between normal strings and verbatim strings. With verbatim strings, just double-up the double-quote like this:

SqlConnection connnection = new SqlConnection(
    @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\Kevin\source\repos\Kops' Toolbox\Kops' Toolbox\Database1.mdf"";Integrated Security=True");

(I removed the space after C:)

Extra tips:

  • SqlConnection and SqlDataAdapter (and SqlCommand, for future reference) are IDisposable, so should be in using blocks.
  • This code is vulnerable to Sql injection attack - please read up about that once you've got this working.
  • You only need the first value from the first row, so try creating an SqlCommand, and use ExecuteScalar.
Richardissimo
  • 5,596
  • 2
  • 18
  • 36