Click here to Skip to main content
15,888,461 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: MVC Razor View and JQuery, how to use @Url.Action Pin
F-ES Sitecore29-Jan-16 3:57
professionalF-ES Sitecore29-Jan-16 3:57 
GeneralRe: MVC Razor View and JQuery, how to use @Url.Action Pin
jkirkerx29-Jan-16 4:14
professionaljkirkerx29-Jan-16 4:14 
Questionautomatic textbox date display Pin
Member 1228733728-Jan-16 8:18
Member 1228733728-Jan-16 8:18 
AnswerRe: automatic textbox date display Pin
F-ES Sitecore28-Jan-16 21:53
professionalF-ES Sitecore28-Jan-16 21:53 
QuestionSqlconnection Pin
Member 1228733728-Jan-16 4:08
Member 1228733728-Jan-16 4:08 
AnswerRe: Sqlconnection Pin
Richard Deeming28-Jan-16 4:26
mveRichard Deeming28-Jan-16 4:26 
GeneralRe: Sqlconnection Pin
Member 1228733728-Jan-16 5:48
Member 1228733728-Jan-16 5:48 
GeneralRe: Sqlconnection Pin
Richard Deeming28-Jan-16 6:10
mveRichard Deeming28-Jan-16 6:10 
You have two options:

1) Define the connection string in the web.config file, and use the name of that entry to retrieve the connection:
XML
<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add
            name="TheEntryName"
            providerName="System.Data.SqlClient"
            connectionString="YOUR CONNECTION STRING HERE"
        />
    </connectionStrings>
</configuration>

C#
using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["TheEntryName"].ConnectionString))
{
    ...
}


2) Hard-code your connection string:
C#
using (SqlConnection connection = new SqlConnection(@"YOUR CONNECTION STRING HERE"))
{
    ...
}


Option #1 is better, because it enables you to update the connection string without recompiling your code. This is particularly important for ASP.NET, where you will potentially have three different versions of the database (development, QA, and live).

If you decide to use option #2, your connection string needs to be a valid C# string literal[^]. You can either escape the backslash character:
C#
"Data Source= VICKIE-PC\\VICTORIASQL2008;Initial Catalog = B.F.M.S; user id = ×××;password=×××"

or use a verbatim string by putting @ before the opening quote:
C#
@"Data Source= VICKIE-PC\VICTORIASQL2008;Initial Catalog = B.F.M.S; user id = ×××;password=×××"


But seriously, use option #1. Smile | :)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Sqlconnection Pin
Member 1228733728-Jan-16 6:46
Member 1228733728-Jan-16 6:46 
GeneralRe: Sqlconnection Pin
Richard Deeming28-Jan-16 6:50
mveRichard Deeming28-Jan-16 6:50 
GeneralRe: Sqlconnection Pin
Member 1228733728-Jan-16 7:30
Member 1228733728-Jan-16 7:30 
Questionhow to generate dll from a project Pin
Member 1219595528-Jan-16 0:54
Member 1219595528-Jan-16 0:54 
AnswerRe: how to generate dll from a project Pin
Richard MacCutchan28-Jan-16 1:17
mveRichard MacCutchan28-Jan-16 1:17 
AnswerRe: how to generate dll from a project Pin
ZurdoDev28-Jan-16 4:35
professionalZurdoDev28-Jan-16 4:35 
QuestionHTTPWebRequest sending right in POST method but manually inserting null Pin
Sachin k Rajput 27-Jan-16 22:46
Sachin k Rajput 27-Jan-16 22:46 
AnswerRe: HTTPWebRequest sending right in POST method but manually inserting null Pin
F-ES Sitecore27-Jan-16 23:50
professionalF-ES Sitecore27-Jan-16 23:50 
Questionhow website will run after losing C# files Pin
Member 1211574627-Jan-16 5:03
Member 1211574627-Jan-16 5:03 
AnswerRe: how website will run after losing C# files Pin
Richard MacCutchan27-Jan-16 5:18
mveRichard MacCutchan27-Jan-16 5:18 
GeneralRe: how website will run after losing C# files Pin
Member 1211574627-Jan-16 5:22
Member 1211574627-Jan-16 5:22 
GeneralRe: how website will run after losing C# files Pin
Kornfeld Eliyahu Peter27-Jan-16 7:47
professionalKornfeld Eliyahu Peter27-Jan-16 7:47 
GeneralRe: how website will run after losing C# files Pin
Richard MacCutchan28-Jan-16 1:14
mveRichard MacCutchan28-Jan-16 1:14 
GeneralRe: how website will run after losing C# files Pin
Kornfeld Eliyahu Peter28-Jan-16 1:16
professionalKornfeld Eliyahu Peter28-Jan-16 1:16 
AnswerRe: how website will run after losing C# files Pin
ZurdoDev27-Jan-16 5:27
professionalZurdoDev27-Jan-16 5:27 
GeneralRe: how website will run after losing C# files Pin
Member 1211574627-Jan-16 5:32
Member 1211574627-Jan-16 5:32 
AnswerRe: how website will run after losing C# files Pin
ZurdoDev27-Jan-16 5:42
professionalZurdoDev27-Jan-16 5:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.