0

I have written code to connect to insert asp.net form data into an SQL server, created in Microsoft Visual Studio 2015, using c#. The issue I am having now is that I am unable to test if form works due to a Synax error. There are spaces between the text file, I am not sure if that might be the reason why there is an error? Or misplaced connection file? I copied the pathing directly from Microsoft Access. Any advice would be greatly appreciated.

Error:

Compiler Error Message: CS1003: Syntax error, ',' expected

Source Error:

Line 11: public partial class _Default : System.Web.UI.Page

Line 12: {

Line 13: SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C: \Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf";Integrated Security=True");

Line 14: protected void Page_Load(object sender, EventArgs e)

Line 15: {

Source File: C:\Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\Default.aspx.cs Line: 13

This is the form code in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;


public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C: \Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf";Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = "insert into Table values('" + pName.Text + "','" + pEmail.Text + "')";
        cmd.ExecuteNonQuery();

        con.Close();
    }
}
0

Use connection strings and the web.config for easing changes.

web.config example:

<configuration>
    <connectionStrings>
        <add name="LoginDB" connectionString="Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\P\\Docs\\Visual Studio 2015\\WebSites\\WebSite2\\App_Data\\Database.mdf;Integrated Security=True" />
    </connectionStrings>
</configuration>

Change your code to:

SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["LoginDB"].ConnectionString);
  • I realized I had an extra quote, I updated code to – penmas Oct 15 '16 at 19:54
0

Should be

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=\"C: \Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf\";Integrated Security=True")

Because it isnt clear for the compiler what you mean. So we use "\" as a signal that the following char should be interpreted as... well a char - and not as a keyword.

Otherwise " is interpreted as start/end of a string which results in string one: "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=\"

"unlogical" data (because not start or end sign): C: \Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf\

and string two: ;Integrated Security=True

Other possibility:

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + @"C: \Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf" + ";Integrated Security=True")

Anyways using a web.config/app.config is way better for your purpose.

  • Thank you for your feedback. When I update the string to the first recommendation you have I get the same error, and when I update it to the second string I get a similar error: "Compiler Error Message: CS1002: ; expected" – penmas Oct 15 '16 at 19:24
  • My mistake was silly, I had an extra quotation mark.... line should have been – penmas Oct 15 '16 at 19:40
  • SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf;Integrated Security=True"); – penmas Oct 15 '16 at 19:41
0

You're using " two times in the same string I don't think they'll work even prepending @. Try using \" also a connection string should be in the app.config or web.config file. Never hardcoded in your source.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.