...

View Full Version : Problem with connection string in visual studio 2010!!!!!



Qx1
05-27-2012, 10:06 PM
hello
i am working on a website..using ASP.net 4.0 and C#.
everything with the designing and layouts is done so i shifted my focus on the database thing and the first day has been the longest yet..
i also have the MS SQL Management server 2008 R2 installed as windows authentication but im not using that.
I am using the inbuilt database service in the visual studio 2010.
what i did was that..
First i created a database by going to "ADD NEW ITEM" and then selecting the "SQL DATABASE SERVER" , inamed it DNS.
the database got created fine. I added some tables and content to it. I the dragged the table in my content place holder and the gridview was fine..when i run the page, the data is displayed fine in the gridview.
The problem is that i am not able to get data into the database..the connection srting seems wrong, my connection string is


SqlConnection con = new SqlConnection("Server=VISHAL-PC;Database=DNS;Integrated Security=True;");
Also in the properties of my database connection string is different..here it is


Data Source=.\SQLEXPRESS;AttachDbFilename="E:\Programing\Projects\DNS Site 2\App_Data\DNS.mdf";Integrated Security=True;User Instance=True
here the server is displayed ".\SQLEXPRESS" but if i write that in my connection string then it shows a red line under the ".\" part and also in the file path.
Even if i dont use the file path and give the database name DNS and server name as SQLEXPRESS then also error occurs at my connection object con.

This is the error i get

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

he error is shown here:


SqlConnection con = new SqlConnection("Server=VISHAL-PC;Database=DNS;Integrated Security=True;");
SqlDataAdapter da;
DataSet ds = new DataSet();


protected void Page_Load(object sender, EventArgs e)
{

}



protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds1 = new DataSet();
SqlCommand cmd = new SqlCommand("Insert into Registration values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "','" + TextBox14.Text + "',),con");
con.Open();
cmd.ExecuteNonQuery();
con.Close();

Response.Write("Data Saved Sucessfully");



}..

Here is the code from my web.config <connectionstring>

<connectionStrings>
<add name="DNSConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DNS.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

im unable to get my connection string..please help me guys..i just have 2-3 days!!

Qx1
05-27-2012, 11:44 PM
i think i got it.. here are the changes i made:
in web.config file
[CODE]<connectionStrings>
<add name="mycon" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DNS.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>[\CODE]

and in my code
[CODE]SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
SqlDataAdapter da;
DataSet ds = new DataSet();


protected void Page_Load(object sender, EventArgs e)
{

}



protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds1 = new DataSet();
SqlCommand cmd = new SqlCommand("Insert into Registration values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "','" + TextBox14.Text + "',),con");
cmd.con.Open();
cmd.ExecuteNonQuery();
con.Close();

Response.Write("Data Saved Sucessfully");



}[\CODE]

but now ther is new error:
[CODE]
InvalidOperationException was unhandled by the user code
ExecuteNonQuery: Connection property has not been initialized.[\CODE]

what is the problem with the ececutenonquery???

alykins
06-04-2012, 12:50 PM
did you ever get this? the problem is in


cmd.con.Open();


needs to be


con.Open();
cmd.ExecuteNonQuery();
.....

gnoix
07-04-2012, 05:00 AM
please try to add <remove name="LocalSQLServer" /> before define your connection string

<connectionStrings>

<remove name="LocalSQLServer" />

<add name="DNSConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DNS.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum