c#


Insert into database c# connection string error


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();
}
}
}
}
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.
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;");
InitialCatalog is two separate words initial catalog.

Related Links

Roslyn Scripting
How to set the value of Process.GetCurrentProcess().PeakWorkingSet64 in C# .NET
Why do I only have a subset of asp.net membership tables?
Linq to SQL Object Class generation
Convert label text to decimal
Function reading line from stream is blocking
When escaping characters in a string, is it faster to test for the need to do so first?
Having trouble displaying a button once when is clicked
Date range overlap issue in LINQ
ASP.net MaskEditExtender stops working when parent div is display:none
pass C# arraylist to Javascript
Regular expression usage
Using LINQ to return List from 2 lists [duplicate]
Session is Closed! NHibernate shouldn't be trying to grab data
Run same exe twice
Is there a language that allows both static and dynamic typing? [closed]

Categories

HOME
asterisk
oop
azure-active-directory
leaflet
virtual-machine
mysqli
bigtable
azure-database-mysql
jquery-plugins
nltk
ubuntu-14.04
esxi
jquery-select2
pyodbc
box-api
fetch
point-of-sale
osrm
ng2-charts
browsermob
webseal
tag-cloud
alert
conditional-formatting
combinations
typedef
concatenation
quality-center
android-wear-2.0
rancher
rollupjs
formtastic
mex
javapns
kundera
jboss-arquillian
vtd-xml
imageprocessor
mapquest
mediastream
aspose.pdf
ssdp
taiga
encoder
synchronized
jenkins-2
yank
react.rb
skype-bots
enet
sp
aot
multi-targeting
critical-section
disassembly
sqlproj
portaudio
easy68k
airwatch
cherry-pick
angularfire
jde
jmap
commercetools
serial-communication
first-order-logic
hexagonal-tiles
tooltwist
p4python
page-refresh
log-analysis
waterline
maven-archetype
indices
nsnotificationcenter
selenium-grid2
javacompiler
reactjs-native
rda
jsmpp
sqldatasource
vbaccelerator
static-class
forward-declaration
mosix
uikit-dynamics
removeall
pageload
ncml
gssapi
ios6-maps
django-1.4
animationdrawable
parsec
datejs
hs-err
s60
mysql-pconnect
html-help-workshop
onresize
weak-typing
urlscan

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App