2

i am encountering an error when trying to set up an insert command into my database, it appears to be with the connection string. I am extremely new to all this and am trying to get the correct code in order to upload into my database and assume that the syntax i am using may be wrong and the cause of the error.

Here is the code a little bit clearer:

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;

namespace ComputingProjectwh.TestPages._1._Further_Mechanics
{
    public partial class Moments_and_Energy_Test1 : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Submit_Click(object sender, EventArgs e)
        {
            if (!this.IsValid)
                return;
            int score = 0;
            List<RadioButtonList> list = new List<RadioButtonList>() { RadioButtonList1, RadioButtonList2, RadioButtonList3, RadioButtonList4, RadioButtonList5, RadioButtonList6, RadioButtonList7, RadioButtonList8, RadioButtonList9, RadioButtonList10 };
            foreach (var element in list)
            {
                if (element.SelectedValue == "Correct")
                {
                    score++;
                }

            }
            Response.Write("you scored: " + score);
            Button1.Visible = false;

            if (score != 0);
            {
                SqlConnection sqlConnection1 = new SqlConnection (@"Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-ComputingProjectwh-20170404101246.mdf;InitialCatalog=aspnet-ComputingProjectwh-20170404101246;IntegratedSecurity=True");

                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "INSERT AspNetUserTestScores (Id, MomentAndEnergyTestScore) VALUES (Id, score)";
                cmd.Connection = sqlConnection1;

                sqlConnection1.Open();
                cmd.ExecuteNonQuery();
                sqlConnection1.Close();


            }

        }
    }
}

enter image description here

I am really not sure what the problem is and cant seem to find an answer on the internet. Any help would be greatly appreciated.

1
  • Refer connectionstrings.com for correct connection string. Commented Apr 9, 2017 at 11:00

2 Answers 2

2

When connecting to MSSQL, there is no initialcatalog, You are using a wrong connection string.

This is the correct syntax:

  Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

Or in your case, for trusted connection:

  Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

With your data:

 SqlConnection sqlConnection1 = new SqlConnection("Server=LocalDb;Database=aspnet-ComputingProjectwh-20170404101246.mdf;Trusted_Connection=True;");
Sign up to request clarification or add additional context in comments.

5 Comments

It doesnt recognise anything in those strings?
like this? SqlConnection sqlConnection1 = new SqlConnection(Server= LocalDb; Database= aspnet - ComputingProjectwh - 20170404101246.mdf; Trusted_Connection = True;);
I think that my connection string is correct but the actual data ive put in is wrong now as im getting an error saying that connection could not be established. is that the correct syntax when connecting to a local server created by asp itself?
Sorry new to this, how do we do that?
1

InitialCatalog is two separate words initial catalog.

2 Comments

Didn't seem to make a difference for me. I assume my syntax is wrong or old
Please see my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.