Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename=|C:Users|blendy|Desktop|projekti|prova capston|capston2|capstone project|capstone project|loginforma.mdf; Integrated Security=True; User Instance=True");
        SqlCommand cmd;
        DataSet ds = new DataSet();


i have this code,
the problem is this:

ArgumentException was unhandled
invalid value key 'attachdbfilename'.

how do i connect to database i have this directory that i have shown there.
Posted
Updated 28-Apr-14 8:10am
v2
Comments
Herman<T>.Instance 28-Apr-14 13:51pm    
AttachDbFilename is for once registration of a database in LocalDB
[no name] 28-Apr-14 14:40pm    
then what should i make?
Herman<T>.Instance 28-Apr-14 14:41pm    
enough examples can be found here:
http://www.connectionstrings.com/sql-server/
[no name] 28-Apr-14 14:53pm    
thanks for your help

see here: http://www.connectionstrings.com/sqlconnection/using-an-user-instance-on-a-local-sql-server-express-instance/[^]
I think AttachDbFilename value is in wrong format. If you still fail to connect, you can try SSMS [^] to connect that database file to see if anything is wrong with it.

[Update]

string connStr = @"Data Source=.\SQLEXPRESS; AttachDbFilename=C:\Users\blendy\Desktop\projekti\prova capston\capston2\capstone project\capstone project\loginforma.mdf; Integrated Security=True; User Instance=True";
SqlConnection con = new SqlConnection(connStr);


I think the above connection string will work. If it doesn't work, attach it to the sql server instance using SSMS. then try this connection string:
string connStr = @"Data Source=.\SQLEXPRESS; Initial Catalog=loginforma; Integrated Security=True; User Instance=True";
 
Share this answer
 
v2
Comments
[no name] 28-Apr-14 14:45pm    
what do you mean the value is wrong? which value? if i make the direction source with this / / / it says the same database with that name already exist and something or the UDC or idk what was that dont let you... something like that
Vedat Ozan Oner 28-Apr-14 15:12pm    
I have updated my answer. Please see it.
[no name] 1-May-14 11:43am    
the connectionstring property has not been initializied

string connStr = @"Data Source=.\SQLEXPRESS; AttachDbFilename=C:\Users\blendy\Desktop\projekti\prova capston\capston2\capstone project\capstone project\Baza1.mdf; Integrated Security=True; User Instance=True";
SqlConnection con = new SqlConnection();
SqlCommand cmd;
DataSet ds = new DataSet();

what have i done wrong?
[no name] 1-May-14 12:20pm    
no problem in the code, just when try to logon or register it says the connectionstring property has not been initializied

you have the code down there.
Vedat Ozan Oner 1-May-14 12:28pm    
https://www.google.com.tr/search?q=sqlconnection+example
See if there's a log file in there and delete it if there is (loginforma.ldf).
 
Share this answer
 
Comments
[no name] 28-Apr-14 14:43pm    
i had some ldf deleted but still nothing, i'll try restart the pc maybe will work something...
this is the code, problem is: the connectionstring property has not been initializied

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string connStr = @"Data Source=.\SQLEXPRESS; AttachDbFilename=C:\Users\blendy\Desktop\projekti\prova capston\capston2\capstone project\capstone project\Baza1.mdf; Integrated Security=True; User Instance=True";
SqlConnection con = new SqlConnection();
SqlCommand cmd;
DataSet ds = new DataSet();



private void btnlogin_Click(object sender, EventArgs e)
{
try
{
if (txtEmri.Text == "" || txtFjalkalimi.Text == "")
{
MessageBox.Show(" Shkruaj Emrin dhe Fjalkalimin.");
return;
}

cmd = new SqlCommand("SELECT * FROM tbl_Regjistrimi where Emri='" + txtEmri.Text + "' and Fjalëkalimi='" + txtFjalkalimi.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if (i == 1)
{
connStr = "Mirë se erdhe " + txtEmri.Text;
this.Hide();
Form3 f3 = new Form3(connStr);
f3.Show();
ds.Clear();
}
else
{
MessageBox.Show("Nuk jeni të regjistruar ose gabim keni shëenuar Emrin/Fjalëkalimin");
txtFjalkalimi.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
 
Share this answer
 
Comments
[no name] 1-May-14 12:21pm    
Why is this a solution? Your error means exactly what it says it should be :
SqlConnection con = new SqlConnection(connStr);
[no name] 1-May-14 12:23pm    
it dont accept (connStr)
Error 1 A field initializer cannot reference the non-static field, method, or property 'capstone_project.Form1.connStr'
[no name] 1-May-14 12:26pm    
That's because you are decaling your variables as class variables. Try:
public Form1()
{
InitializeComponent();
con = new SqlConnection(connStr);
}
[no name] 1-May-14 12:32pm    
if i put there it dont recognize the con
[no name] 1-May-14 17:20pm    
I don't have any idea what you are doing. I copied your code and pasted it into a new project and it worked just fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900