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

More efficient UI Overlay in 2D Unity game
CRUD filtering/sorting doesnt work in asp.net
What argument should I use with SqlFunctions.DateName to get the month like “MM”?
Diagonal match in Connect 4
DataTrigger storyboard not firing WPF on PropertyValueChange
c# list index 0 showing value as -1
Include subset of nuget package in class library
Can Blend for Visual studio 2017 import psd file?
blocking all mapping drives and direct shared access programmatically
SSH.net - SSH connection works, but SCP returns “incorrect username and password”
Execute private method in C# interactive window
Could not AOT the assembly System.ComponentModel.DataAnnotations.dll
How work network paths in C#?
issue regarding sql commands in xamarin new update in visual studio 2017
Singleton with DbContext - creating instance in Startup.cs
Check if key has been pressed Selenium

Categories

HOME
aix
watson-iot
coordinates
mailchimp
ebay
sdk
aggregate-functions
collision-detection
browserstack
minimax
openbsd
email-attachments
web-worker
css-float
smooth-scrolling
filehelpers
vivado-hls
jquery-easyui
md2
autologin
ampscript
jq
gomobile
fastboot
tcpdf
apdu
aspxgridview
kundera
spring-data-gemfire
folder
ng2-bootstrap
wercker
ilog
offset
bootcamp
headphones
dom4j
pymunk
azure-storage-files
yelp
instances
latency
pyexcel
rexx
servicebus
gradlew
synchronized
opos
confluent
evaluation
dotnet-cli
px4
node-java
boost-regex
webmatrix
opticalflow
suitesparse
google-scholar
racket-student-languages
boost-range
foreground
geokit
particle-system
syslog4j
nsoperationqueue
jde
pebble-js
web-optimization
methodology
jersey-test-framework
wiql
mojolicious
javapackager
taverna
dnvm
textblob
chrome-mobile
xcode5
grunt-contrib-connect
nsurlrequest
indic
sendy
nodeunit
pthreads-win32
friendly-id
misfire-instruction
ironworker
azure-pack
nsautoreleasepool
string-length
database-restore
moonscript
wpml
waveout
parsec
virtual-printer
qvariant
oaw
pitch-shifting
miniport
large-data-volumes
glassfish-embedded
authkit
j++

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