Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to connect to my database from another pc but i get error and when i connect from my laptop it works perfectly how i can fix it

What I have tried:

I try to creat another database i checked the connection but its not working
Posted
Updated 24-Sep-22 4:39am
Comments
Dave Kreskowiak 23-Sep-22 22:38pm    
Which database engine are you using? There is probably configuration that has to be done to accept remote connections.

Also, you will probably have to open the ports of whatever firewall you're using on the machine hosting the database.

But, all of this depends on the database engine you're using.
Kamel Soltani 24-Sep-22 5:25am    
I Use Microsoft SQL Server Database File (SqlClient)

It depends on a couple of factors: what type of database you are using is the main one.

If it is a multiuser DB like SQL Server or MySql, then you need to connect to the correct instance of the server, and then the right DB within that server - and that means that the server application needs to be configured to allow remote connections, and also to allow the login type and data you are providing. Generally, that means creating an SQL Server / MySQL user with a password and using the right connections string to access the DB: the user will need the right access permissions to the DB as well.

If it is a single user DB like Access, SqLite, or similar then the folder it resided in on your computer needs to be shared with the network and the appropriate write permissions set up for it: it's often easiest to map the folder as a drive on the remote machine as that makes the connection easier later. This will require some changes in configuration for Windows, probably on both machines.

Obviously, in either case you need to be using the right classes in your code to connect to the DB: SqlConnection, MySqlConnection, OleDBConnection, etc., and the related Command, DataDadapter, DataReader classes to process actual queries.

And remember: "i get error" tells us nothing about what is happening, or when it happens - and that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Show us relevant code fragments!
 
Share this answer
 
Comments
OriginalGriff 24-Sep-22 5:49am    
It's simple: you are trying to access "LocalDB" - which is always on the current PC, not the remote one.

You need to change your connection string to "point" at the actual SQL Server instance that has the DB ... This may help:

https://www.codeproject.com/Tips/1198443/Simple-SQL-Connection-String-Creation
Kamel Soltani 24-Sep-22 8:10am    
I try it but its not working can i send you my project to take look on it im beginner in this and i don't know what to do please help 🥺
OriginalGriff 24-Sep-22 8:22am    
No point: we can't run your code under your system - we have no access to your local network or the computers attached to it, and that's what you need to even start working out the connection string.

Try the link I gave you earlier: I just did and got a valid connection string for my local systems, even 5 years later and under Win 11!
First, you're using LocalDB, which cannot receive connections from remote machines. You have to move your database to SqlExpress, which was probably installed along-side LocalDB.

The next problem is you have to install SQL Server Management Studio[^].

When that is done, you have to start the SQL Server Configuration Manager where you'll have to turn on TCP/IP connections to your SqlExpress server.

Next, you'll probably need to open the ports for SQL Server in Windows Firewall[^].

Oh, and you'll need to change that connection string to point to your server, which you can find details on at Microsoft SqlClient Data Provider for SQL Server Connection Strings - ConnectionStrings.com[^].

You can NOT use TrustedConnection because you're probably not running in an Active Directory domain environment, so you'll have to use a username and password for SQL login. You'll also have to setup that username and password in SQL Server Manager for your database.
 
Share this answer
 
Comments
Kamel Soltani 24-Sep-22 19:12pm    
I use local database and it's installed when i install the app in my friend the problem is the app doesn't use that local base
Dave Kreskowiak 24-Sep-22 19:16pm    
You just completely changed the question. Now you're saying your friend can't use the database on his machine when your original question said you wanted him to connect to the database on your machine.

Well, you never said what error you friend is getting, but I'm willing to bet he doesn't have the LocalDB server installed on his machine.
Kamel Soltani 25-Sep-22 4:35am    
Its a local data base that's what i mean i'm sorry i don't mean to confuse you
Kamel Soltani 25-Sep-22 6:14am    
In the title i said local database
<pre lang="C#"><pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sell
{
    public partial class category : Form
    {
        public category()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            log.Minimaze(this);
        }
        private void populate()
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sell.Properties.Settings.DatabaseConnectionString"].ConnectionString))
            {
                con.Open();
                string query = "select * from CategoryTbl ";
                SqlDataAdapter sda = new SqlDataAdapter(query, con);
                SqlCommandBuilder builder = new SqlCommandBuilder(sda);
                var ds = new DataSet();
                sda.Fill(ds);
                CatDGV.DataSource = ds.Tables[0];
                con.Close();
            }
        }
        private void label6_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }

        private void guna2DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            catid.Text = CatDGV.SelectedRows[0].Cells[0].Value.ToString();
            catnom.Text = CatDGV.SelectedRows[0].Cells[1].Value.ToString();
            catdis.Text = CatDGV.SelectedRows[0].Cells[2].Value.ToString();
        }

        private void guna2Button6_Click(object sender, EventArgs e)
        {
            if(catid.Text!="" && catnom.Text != "") { 
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sell.Properties.Settings.DatabaseConnectionString"].ConnectionString)) { 
                try
                {
                    con.Open();
                    string query = "insert into CategoryTbl values (" + catid.Text + ",'" + catnom.Text + "','" + catdis.Text + "')";
                    SqlCommand cmd = new SqlCommand(query, con);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("catégorie ajoutée avec succès");

                    con.Close();
                    

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    con.Close();
                }
            }
            populate();
            catid.Text = "";
            catnom.Text = "";
            catdis.Text = "";
        }else
            {
                MessageBox.Show("information manquante");
            }
        }
        private void guna2Button5_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sell.Properties.Settings.DatabaseConnectionString"].ConnectionString))
                {
                try
                {
                    if (catid.Text == "" || catnom.Text == "")
                    {
                        MessageBox.Show("information manquante");
                    }
                    else
                    {

                        con.Open();
                        string query = "update CategoryTbl set Nom ='" + catnom.Text + "',Description='" + catdis.Text + "'where Id =" + catid.Text + ";";
                        SqlCommand cmd = new SqlCommand(query, con);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("catégorie modifier avec succès");
                        con.Close();
                        populate();
                    
                }
                 }
            catch (Exception ex)
                {
                MessageBox.Show(ex.Message);
                con.Close();
             }
        }
            populate();
            catid.Text = "";
            catnom.Text = "";
            catdis.Text = "";
        }

        private void guna2Button7_Click(object sender, EventArgs e)
        {
            using (SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["sell.Properties.Settings.DatabaseConnectionString"].ConnectionString))
            {
                try
            {
                if (catid.Text == "")
                {
                    MessageBox.Show("Sélectionner la catégorie à supprimer");
                }
                else
                {
                    Con.Open();
                    string query = "delete  from  CategoryTbl where Id =" + catid.Text + "";
                    SqlCommand cmd = new SqlCommand(query, Con);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("catégorie suppremer avec succès");
                    Con.Close();
                    populate();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Con.Close();
            }
        }
            populate();
            catid.Text = "";
            catnom.Text = "";
            catdis.Text = "";
        }

        private void guna2Button3_Click(object sender, EventArgs e)
        {
            stock st = new stock();
            st.Show();
            this.Hide();
        }

        private void guna2Button9_Click(object sender, EventArgs e)
        {

        }

        private void guna2Button8_Click(object sender, EventArgs e)
        {
            history history = new history();
            history.Show();
            this.Hide();
        }

        private void logout_Click(object sender, EventArgs e)
        {
            log log = new log();
            log.Show();
            this.Hide();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void guna2Button2_Click(object sender, EventArgs e)
        {
            sell sel = new sell();
            sel.Show();
            this.Hide();
        }

        private void guna2TextBox2_TextChanged(object sender, EventArgs e)
        {

        }
        private void category_Load(object sender, EventArgs e)
        {
            populate();

        }

        private void guna2Panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}

the problem is shown when I install the app on another computer, the laptop don't use the local database I made, and this is the app.config file
XML
<pre><?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="sell.Properties.Settings.DatabaseConnectionString"
            connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
</configuration>
 
Share this answer
 

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