Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys i get stuck so badly
i created a windows form with sql express with visual studio 2017 .
the application work nicely in my own pc but when i published it and instal it another pc , i get error a network related error 52.
consider it that i have installed sql express on clinet pc.
this link to my project

Hamkar_Vendors - Download - 4shared[^]

this my code

C#
using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Hamkar_Vendors
    {
        public partial class MainForm : Form
        {
            //static string path = Path.GetFullPath(Environment.CurrentDirectory);
            //static string databaseName = "Hamkar_Vendor.mdf";
            string con_string = @"Data Source=.\SQLEXPRESS2014;Initial Catalog=hamkar_Vendor1;Integrated Security=True";
    
            SqlDataAdapter dataAdapter;
            DataTable dataTable;
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                vendorGrp.Hide();
                Getdata("select * from vendor");
                
    
    
            }
    
            private void Getdata(string selectedString)
            {
                try { 
                dataAdapter = new SqlDataAdapter(selectedString,con_string );
                dataTable = new DataTable();
                dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(dataTable);
                bindingSource1.DataSource = dataTable;
                    }
                catch(Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
    
            private void ثبتاطلاعاتToolStripMenuItem_Click(object sender, EventArgs e)
            {
                vendorGrp.Show();
            }
    
            private void خروجToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void submitBtn_Click(object sender, EventArgs e)
            {
                SqlCommand command;
                string insert = @"insert into vendor(Address , Company_Name , Country , Email , Fax , Industry , Phone , Resume , ResumeWithUs , TypeOfItems , TypeOfServices , WebSite) 
                  values (@Address , @Company_Name , @Country , @Email , @Fax , @Industry , @Phone , @Resume , @ResumeWithUs , @TypeOfItems , @TypeOfServices , @WebSite) ";
    
                using (SqlConnection con = new SqlConnection(con_string))
                {
                    try
                    { 
                    con.Open();
    
                    command = new SqlCommand(insert, con);
                    command.Parameters.AddWithValue(@"Address", AddressTxt.Text);
                    command.Parameters.AddWithValue(@"Company_Name", CompanyTxt.Text);
                    command.Parameters.AddWithValue(@"Country", CompanyTxt.Text);
                    command.Parameters.AddWithValue(@"Email", EmailTxt.Text);
                    command.Parameters.AddWithValue(@"Fax", FaxTxt.Text);
                    command.Parameters.AddWithValue(@"Industry", IndustryTxt.Text);
                    command.Parameters.AddWithValue(@"Phone", TelephoneTxt.Text);
                    command.Parameters.AddWithValue(@"Resume",Resumetxt.Text );
                    command.Parameters.AddWithValue(@"ResumeWithUs", ResumeWustxt.Text);
                    command.Parameters.AddWithValue(@"TypeOfItems", ItemsTxt.Text);
                    command.Parameters.AddWithValue(@"TypeOfServices", servicetxt.Text);
                    command.Parameters.AddWithValue(@"WebSite", WebSiteTxt.Text);
                    
                    command.ExecuteNonQuery();
                    }
                    
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
    
                    Getdata("select * from vendor");
                    dataGridView1.Update();
                }
    
    
            }
    
            private void vendorGrp_Enter(object sender, EventArgs e)
            {
                dataGridView1.DataSource = bindingSource1;
            }
    
            private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
    
            }
    
            
    
            private void toolStripMenuItem1_Click(object sender, EventArgs e)
            {
               
            }
    
            private void جستجویاطلاعاتToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                
            }
    
            private void DeleteBtn_Click(object sender, EventArgs e)
            {
                
    
                DataGridViewRow row = dataGridView1.CurrentCell.OwningRow;
                string value = row.Cells["id"].Value.ToString();
                string cname = row.Cells["Company_Name"].Value.ToString();
                string address = row.Cells["Address"].Value.ToString();
                DialogResult dialogresualt = MessageBox.Show("آیا از حذف رکورد مطمئن هستید","پیغام", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                string DeleteStatment = "Delete from vendor where id = '" + value + "'";
                if(dialogresualt == DialogResult.Yes)
                {
                    using (SqlConnection con = new SqlConnection(con_string))
                    {
                        try
                        { 
                        con.Open();
                        SqlCommand com = new SqlCommand(DeleteStatment, con);
                        com.ExecuteNonQuery();
                            Getdata("select * from vendor");
                            dataGridView1.Update();
                              
                        }
    
                        catch(Exception ed)
                        {
                            MessageBox.Show(ed.Message);
                        }
                    }
                }
            }
    
            private void SearchBtn_Click(object sender, EventArgs e)
            {
                //WindowState = FormWindowState.Maximized;
                //vendorGrp.Width = this.Width;
                //dataGridView1.Width = this.Width -200;
    
                switch(searchcombo.SelectedItem.ToString())
                {
                    case "نام شرکت":
                        Getdata("select * from vendor where lower(Company_Name) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "تلفن":
                        Getdata("select * from vendor where lower(Phone) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "ایمیل":
                        Getdata("select * from vendor where lower(Email) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "فکس":
                        Getdata("select * from vendor where lower(Fax) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "وب سایت":
                        Getdata("select * from vendor where lower(WebSite) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "زمینه فعالیت":
                        Getdata("select * from vendor where lower(TypeOfService) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "صنعت":
                        Getdata("select * from vendor where lower(Industry) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "محصولات":
                        Getdata("select * from vendor where lower(TypeOfItems) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                }
                
                
            }
        }
    }


What I have tried:

i tried almost everything
like enabling all the sql server express services on client pc
Posted
Updated 6-Mar-18 21:50pm
v2
Comments
RickZeeland 7-Mar-18 5:20am    
You could install SQL Server Management Studio, and see what the Server connection is by clicking on Server in the tree, also do a test query ...

1 solution

C#
string con_string = @"Data Source=.\SQLEXPRESS2014;Initial Catalog=hamkar_Vendor1;Integrated Security=True";

This path '.\SQLEXPRESS2014' is to local storage, you need to change it to target server.
C#
Getdata("select * from vendor where lower(Company_Name) like '%" + searchtxt.Text.ToLower() + "%'");

Why are doing this as your about usage of parameters?

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
Comments
robertofantasy 7-Mar-18 4:08am    
firstly : this is a local database that security is my least concern.
second : by changing to target pc , you mean for every one that want to install
my app on their pc , i need to published a seperate database or is there way to use same connection string that work for every pc
Patrice T 7-Mar-18 4:22am    
If the database is local to your PC, it will be local to your client PC.
So your need to give them an empty database at same path and you need to make sure they have an SQL server in their PC.
robertofantasy 7-Mar-18 4:27am    
ok. so my connection string is right (@"Data Source=.\SQLEXPRESS2014;Initial Catalog=hamkar_Vendor1;Integrated Security=True";) and i install sql server express on my target pc but when i run the program i get error
a network related ... error 52
robertofantasy 7-Mar-18 4:27am    
where is the problem

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