0
votes

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();
    }
}
3

3 Answers

0
votes

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.

0
votes

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.

0
votes

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);