So I'm working on making a submit content page that will store the users input into a database, here is the code I have so far:
protected void submitData_Click(object sender, EventArgs e)
{
string userId = HttpContext.Current.User.Identity.Name;
int categoryId = Convert.ToInt32(categories.SelectedValue);
if (categoryId > 0 && content.Text != "" && description.Text != "")
{
using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C: \Users\Will\Documents\Visual Studio 2015\WebSites\inshort\App_Data\ASPNETDB.MDF";Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand("INSERT INTO aspnet_newscontent (author, username, date, category, content, description) VALUES (@author, @username, @date, @category, @content, @description)");
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@author", nameInput.Text);
cmd.Parameters.AddWithValue("@username", userId);
cmd.Parameters.AddWithValue("@date", DateTime.Today);
cmd.Parameters.AddWithValue("@category", categoryId);
cmd.Parameters.AddWithValue("@content", content.Text);
cmd.Parameters.AddWithValue("@description", description.Text);
connection.Open();
cmd.ExecuteNonQuery();
}
}
else
{
errorLabel.Text = "Please fill in the required fields.";
}
}
However, I am getting an error saying that the connection string contains an invalid character "\", which makes sense but whenever I go to my database's properties and look at the Connection String property, that is what it says.
I am using Microsoft Sql Server Express to have the database locally hosted if that changes anything. Anyone know how to format these connection strings?