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

How to pass parameters to jQuery document.ready() function (ASP.NET MVC, C#)
Cannot create an instance of the abstract class or interface 'System.Drawing.Image'
Writing to a text file at specific position?
Convert Audio File to text using System.Speech
Reactive Extensions (Rx) Misses event
How to get a value from resource file using its key
How to write all files Contain in the subfolders and in the folders in to a .txt file
C# equivalent of VB DLL function declaration (InternetSetOption)?
Custom control doesn't expose image properties in the Designer
MEF Property Allways Returning Null
Insert attribute into HTML elements with regex C#
Create a derived class object from a base class object
How to achieve reversed document ordered XmlDataSource in ListView?
C#: Return IEnumerable when field is not null?
Multiple SQL queries asp.net c#
Re-using a font object in iTextSharp

Categories

HOME
ide
process
window.open
pandoc
squeak
opengl-es
css-selectors
powerquery
uml-designer
docker-compose
markdown
select2
expo
watson-dialog
tag-cloud
bookmarklet
windows-phone
airbnb
novacode-docx
printf
single-page-application
computation-theory
minimagick
git-branch
matlab-gui
screen-scraping
email-client
advertising
priority-queue
akavache
jboss-arquillian
schedule
multilanguage
kubectl
apache-falcon
bootcamp
matlab-compiler
dropdownbox
var
instances
ssdp
compiled-query
autodesk-data-management
nshttpurlresponse
risc
line-endings
stream-socket-client
runtime-permissions
programming-pearls
portaudio
android-music-player
firebase-job-dispatcher
konakart
qtruby
android-progressbar
periodic-processing
factorization
code-translation
containstable
dday
markerspiderfier
jquery-draggable
producer-consumer
event-tracking
page-flipping
tokudb
playn
waterline
teamwork
opennebula
nfa
perfect-scrollbar
teamstudio-unplugged
optimistic-locking
rails-migrations
yandex-api
image-preloader
fastcgi-mono-server
cvi
vbaccelerator
maven-webstart-plugin
windows-scheduler
coinbase-php
pymel
cron4j
tasklist
report-viewer2010
motordriver
flexbuilder
windows-xp-embedded
jqtransform
netduino
quazip
noise-reduction
html5-animation
excellibrary
llblgen
fxcopcmd
carbon-emacs
ihtmldocument2
projectgen
wtsapi32

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