I have created Table in SQL server with visual studio 2005 Table name Student
and table data ID,name, surname, indeks, years,
I have this code in C#

protected void Button1_Click(object sender, EventArgs e)
    {
       // try
        //{
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"|DataDirectory|\\db_lab8.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True");
              //                                   ("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"|DataDirectory|\\db_lab8.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True");
            conn.Open();
            //              
            SqlCommand a = new SqlCommand("INSERT INTO Student(ID,name, surname, indeks, years) VALUES ('" + TextBox5.Text + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')", conn);
            a.Connection.Open();
            a.ExecuteNonQuery();
            a.Dispose();
            a.Connection.Close();
            conn.Close();
            Response.Redirect("Default.aspx");
       // }
        //catch (Exception eee)
        //{
         //   Console.WriteLine("Generic Exception Handler: {0}", eee.ToString());
        //}

        
    }

I have tried with try and catch but it doesn't works OK
it just shows the browser and it executes the catch block

without try and catch it shows bug at the line: conn.Open();
and it says

A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Recommended Answers

All 3 Replies

Database file is not found.

SqlConnection conn;
conn= new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\db_lab8.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

does it work OK?

Yes it does thnx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.