Question

LloydyJ89 on Thu, 19 Jan 2017 13:11:19


Ive created a new project and added a database which is saved locally on the C drive. When running the program i can see the data displayed however when im trying to search (click button) for a certain ID number it throws up an exception 

The code i entered so far is below 

 private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=\\ox15156\\c$\\Users\\LLOYDJ\\Documents\\WindowsFormsApplication1\\WindowsFormsApplication1\\Database1.mdf;Integrated Security=True");
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter ("select * from Table where Id like = "+ int.Parse(textBox1.Text), conn);
            sda.Fill(dt);
            dataGridView1.DataSource = dt; 

The error shown is

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: An attempt to attach an auto-named database for file \ox15156\c$\Users\LLOYDJ\Documents\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Can anyone please help



Sponsored



Replies

Carlos J. Quintero on Thu, 19 Jan 2017 18:20:40


Hi, 

This forum is about Visual Studio extensibility (packages, add-ins, MEF, etc.) and your question seems unrelated to that. Please post in a database related forum.

Zhanglong Wu on Mon, 23 Jan 2017 03:24:45


Hi LloydyJ89,

Based on the error message, it seems that the program have not permission to access the localdb, please check if you could access the mdf file via file explorer.

Best regards,

Cole Wu