-2

When I run this program on my device, it works without any problems, but when I transfer it to another device, it does not work.

Where is the problem?

I changed the connection method in other ways and it didn't work either

Error screenshot:

My source of contact data

class DbConn
{
    public SqlConnection conn;

    public DbConn()
    {
        conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Database.mdf;Integrated Security=True");
    }

    public SqlConnection connect()
    {
        conn.Open();
        return conn;
    }

    public void disconnect()
    {
        if (conn.State == ConnectionState.Open == true)
            conn.Close();
    }
}

Form1 Load code:

    private void Form1_Load(object sender, EventArgs e)
    {
        DbConn db = new DbConn();

        string SQL = "select * from Employees";
        db.disconnect();

        SqlCommand cmd = new SqlCommand(SQL, db.connect());
        SqlDataReader Rd = cmd.ExecuteReader();

        DataTable dt = new DataTable();
        dt.Load(Rd);

        dataGridView1.DataSource = dt; 
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
HARI
  • 1
  • 1
  • 1
    Perhaps the database file itself isn't on both devices? This part: `AttachDbFilename=C:\Database.mdf` You don't have anything showing the creation for the database. – Ibrennan208 Jun 28 '22 at 21:02
  • 3
    Probably because that file doesn't exist on the other machine. You should also wrap your connection and command objects with a USING statement. – Sean Lange Jun 28 '22 at 21:04
  • @Ibrennan208 It is in the requested file – HARI Jun 28 '22 at 21:08
  • @SeanLange Yes, it is there, check the picture – HARI Jun 28 '22 at 21:09
  • 2
    As per the question guide, please do not post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. – Dale K Jun 28 '22 at 21:10
  • 3
    It's also poor practice to try to re-use the same connection object throughout your app. It interferes with a feature called **connection pooling** such that is makes your connections slower and use more memory. It also forces things to run in serial that could otherwise be done in parallel/async. In other words, **it really is better to create a brand new connection object for most queries.** – Joel Coehoorn Jun 28 '22 at 21:14
  • @DaleK ok sir nb – HARI Jun 28 '22 at 21:14
  • 2
    @HARI you don't need to call anyone on this site sir. – Dale K Jun 28 '22 at 21:14
  • @DaleK I said it out of respect, thank you for the warning – HARI Jun 28 '22 at 21:18
  • @JoelCoehoorn I have tried other methods and they work well on my device, but when I transfer the program to another device with a data file it does not work – HARI Jun 28 '22 at 21:20
  • Is SQL Server Express installed on the other machine? – D Stanley Jun 28 '22 at 21:36
  • @DStanley no you have link ? – HARI Jun 28 '22 at 21:48
  • There are a bunch of different issues here: LocalDB is clearly not installed on the other machine. You might want to install it, or you might want a centralized SQL Server installation. `AttachDbFilename` is a bad idea and deprecated, create a normal database instead. Connection, command and reader objects must be in `using`, do not cache them. Don't `select *` just select the columns you need. – Charlieface Jun 29 '22 at 09:21

1 Answers1