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'.
Loading


Santhosh NRank #4,788 · 325 reputationPosted Jan 21, 2010, 7:53 PM EST
try to login to the instance first with userrname and password and provide the same in the connection string and check
Lalit MRank #1,186 · 1.5K reputationPosted Jan 21, 2010, 8:16 PM EST
http://blogs.msdn.com/sql_protocols/archive/2006/02/21/536201.aspx
Dipa AhujaRank #1,168 · 1.5K reputationPosted Jan 22, 2010, 6:53 AM EST
n i m not getting help anywhere... :(
Jose24 reputationPosted Jan 22, 2010, 6:58 AM EST
You can also test your connectivity to the database by going to the Data sources (ODBC) snapin from the Control Panel > Administrative tools. From there, you can click on the system DSN tab, and add new. Select SQL Server at the bottom of the list, then click Finish. On the next screen give it a name (any name will do) and make sure the local server ".\SQLEXPRESS" is shown in the last field, then click Next. Now you get to the authentication part. Make sure the first option is selected (NT authentication), and check the box below and click Next. At this point, if you are authenticated to the SQL server instance, you should see a combo box with the list of databases (after checking Change default database to). If you get this far, select the database you are trying to connect to and click next and then finish. On the final screen, click the Test button to see if the connection is successful. If not, try making the tweaks from the SQLExpress instance. Might need to add the user role to that database if it does not have access.
Dipa AhujaRank #1,168 · 1.5K reputationPosted Jan 23, 2010, 2:44 AM EST
bt i dont know how to write in c# :(
Jose24 reputationPosted Jan 27, 2010, 5:24 AM EST
Try editing your connection string to something like:
Jose