Dipa Ahuja

Dipa Ahuja

  • NA
  • 3.1k
  • 715.4k

sql error: cannot open database

Jan 21 2010 12:37 PM
hi... i m getting error like this in my code


  public partial class items : Form
    {
        string con = "Server=.\\SQLEXPRESS;Database=|DataDirectory|/db1.mdf;Trusted_Connection=True;";

     
        SqlDataAdapter da = new SqlDataAdapter();
        SqlConnection objconnection;

     
        private void btnshow_Click(object sender, EventArgs e)
        {
            objconnection = new SqlConnection(con);
            objconnection.Open(); <-------ERROR

            string ss=comboBox1.SelectedValue.ToString();
            string q = "select * from items where cname= " + "'" + ss.ToString() + "'";

            SqlCommand comm = new SqlCommand("select * from items where cname=@cname", objconnection);
            comm.Parameters.AddWithValue("@cname", ss.ToString());
            comm.ExecuteNonQuery();
           
            textBox1.Text = comboBox1.SelectedValue.ToString();

            da = new SqlDataAdapter(q, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGrid1.DataSource = ds.Tables[0];


           

        }


Error
Cannot open database "|DataDirectory|/db1.mdf" requested by the login. The login failed.
Login failed for user 'AHUJA-2FD08D31D\Ahuja's'.





Answers (6)