Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-27-2012, 09:03 PM   #1
Qxx1
Registered User
 
Join Date: Mar 2012
Posts: 9
Default Problem with connection string in visual studio 2010

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

Code:
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

Code:
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
Code:
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:
Code:
 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>
Code:
<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!!
Qxx1 is offline   Reply With Quote
Old 05-27-2012, 10:43 PM   #2
Qxx1
Registered User
 
Join Date: Mar 2012
Posts: 9
Default

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=|DataDirector y|\DNS.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>[\CODE]

and in my code
[CODE]SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["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 to do now??
Qxx1 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:16 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.