Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Qx1
    Qx1 is offline
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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!!

  • #2
    Qx1
    Qx1 is offline
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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???

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    did you ever get this? the problem is in
    cmd.con.Open();
    needs to be
    Code:
    con.Open();
    cmd.ExecuteNonQuery();
    .....

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #4
    New to the CF scene
    Join Date
    Jul 2012
    Location
    australia
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •