Who can teach me how to make Class to store database location When want to use just call the location.

Everytime I will write again the connection. When database location is change, I will change the database location one by one. This is time consuming and difficult to maintain program.

I'm using C# Microsoft visual studio 2008 Windows Form Application

Example:

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.OleDb;

namespace WindowsFormsApplication1.PDC
{
    public partial class AddProduct : Form
    {
        OleDbCommand sCommand;
        OleDbDataAdapter sAdapter;
        OleDbCommandBuilder sBuilder;
        DataSet sDs;
        DataTable sTable;
        string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source=\\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";

        public AddProduct()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OleDbConnection conAuthor;
            string conString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source= \\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";
            conAuthor = new OleDbConnection(conString);

            OleDbCommand cmdInsert, cmdCheckID;
            OleDbDataReader dtrCheckID;

            conAuthor.Open();

            string strChechID = "select * from [Product] where Model='" + txtModel.Text + "'and FGrade='" + txtFGrade.Text + "' and Thk='" + txtThk.Text + "'and FSize='" + txtSize.Text + "'";
            conString = "Insert Into [Product]([Model],[FGrade], [Thk], [FSize]) Values(?,?,?,?)";

            cmdCheckID = new OleDbCommand(strChechID, conAuthor);
            cmdInsert = new OleDbCommand(conString, conAuthor);

            dtrCheckID = cmdCheckID.ExecuteReader();
            if (String.IsNullOrEmpty(txtModel.Text))
            {
                MessageBox.Show("Model field is empty");
                conAuthor.Close();
                dtrCheckID.Close();
            }
            if (String.IsNullOrEmpty(txtFGrade.Text))
            {
                MessageBox.Show("Foam Grade field is empty");
                conAuthor.Close();
                dtrCheckID.Close();
            }
            if (String.IsNullOrEmpty(txtThk.Text))
            {
                MessageBox.Show("Thick field is empty");
                conAuthor.Close();
                dtrCheckID.Close();
            }
            if (String.IsNullOrEmpty(txtSize.Text))
            {
                MessageBox.Show("Size field is empty");
                conAuthor.Close();
                dtrCheckID.Close();
            }

            try
            {

                if (dtrCheckID.HasRows)
                {
                    MessageBox.Show("Duplicate Data!!!!!!");

                    dtrCheckID.Close();
                }
                else
                {
                    dtrCheckID.Close();

                    cmdInsert.Parameters.AddWithValue("@Model", txtFGrade.Text);
                    cmdInsert.Parameters.AddWithValue("@FGrade", txtFGrade.Text);
                    cmdInsert.Parameters.AddWithValue("@Thk", txtThk.Text);
                    cmdInsert.Parameters.AddWithValue("@FSize", txtSize.Text);


                    cmdInsert.ExecuteNonQuery();

                    conAuthor.Close();
                    txtModel.Text = "";
                    txtFGrade.Text = "";
                    txtThk.Text = "";
                    txtSize.Text = "";
                    timer1.Enabled = true;
                }
            }
            catch (Exception)
            {
                //MessageBox.Show("ex.ToString()");
            }
        }

        private void AddProduct_Load(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM Product ORDER BY Model";
            OleDbConnection connection = new OleDbConnection(ConnectionString);
            connection.Open();
            sCommand = new OleDbCommand(sql, connection);
            sAdapter = new OleDbDataAdapter(sCommand);
            sBuilder = new OleDbCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "Product");
            sTable = sDs.Tables["Product"];
            connection.Close();
            dataGridView1.DataSource = sDs.Tables["Product"];
            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM Product ORDER BY Model";
            OleDbConnection connection = new OleDbConnection(ConnectionString);
            connection.Open();
            sCommand = new OleDbCommand(sql, connection);
            sAdapter = new OleDbDataAdapter(sCommand);
            sBuilder = new OleDbCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "Product");
            sTable = sDs.Tables["Product"];
            connection.Close();
            dataGridView1.DataSource = sDs.Tables["Product"];
            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            timer1.Enabled = false;
        }

        private void delete_btn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                sAdapter.Update(sTable);
            }
        }

        private void new_btn_Click(object sender, EventArgs e)
        {
            dataGridView1.ReadOnly = false;
            save_btn.Enabled = true;
            new_btn.Enabled = false;
            delete_btn.Enabled = false;
            btnRefresh.Enabled = false;
            btnAdd.Enabled = false;

        }

        private void save_btn_Click(object sender, EventArgs e)
        {
            sAdapter.Update(sTable);
            dataGridView1.ReadOnly = true;
            save_btn.Enabled = false;
            new_btn.Enabled = true;
            delete_btn.Enabled = true;
            btnAdd.Enabled = true;

            btnRefresh.Enabled = true;

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM Product ORDER BY Model";
            OleDbConnection connection = new OleDbConnection(ConnectionString);
            connection.Open();
            sCommand = new OleDbCommand(sql, connection);
            sAdapter = new OleDbDataAdapter(sCommand);
            sBuilder = new OleDbCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "Product");
            sTable = sDs.Tables["Product"];
            connection.Close();
            dataGridView1.DataSource = sDs.Tables["Product"];
            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            txtFGrade.Text = "";
            txtThk.Text = "";
            txtSize.Text = "";
        }


    }
}

Recommended Answers

All 19 Replies

Use A class file to save the location of your data base and create a property to get and set the location of your database when ever require .......

class database
    {
        string Con_string = "";
        public string location
        {
            get
            {
                return Con_string;
            }
            set
            {
                Con_string = value;
            }
        }
    }

create a object of that class to get and set the property of location
Feel free to ask any problem
Best of luck......

Use A class file to save the location of your data base and create a property to get and set the location of your database when ever require .......

class database
    {
        string Con_string = "";
        public string location
        {
            get
            {
                return Con_string;
            }
            set
            {
                Con_string = value;
            }
        }
    }

create a object of that class to get and set the property of location
Feel free to ask any problem
Best of luck......

Thank you u reply.
Hello expert, is like there
But I don't know how to call the location into other form. Hope u can give me example.

class database
{
string Con_string = "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source=\\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";

public string location
{
get
{
return Con_string;
}
set
{
Con_string = value;
}
}
}

Use A class file to save the location of your data base and create a property to get and set the location of your database when ever require .......

class database
    {
        string Con_string = "";
        public string location
        {
            get
            {
                return Con_string;
            }
            set
            {
                Con_string = value;
            }
        }
    }

create a object of that class to get and set the property of location
Feel free to ask any problem
Best of luck......

Here I attach a file, see u can help me modify and send back to me. Therefore, I can more clearly to know from example.

This file I change type from rar to zip for upload purpose. If cannot open change back to rar format type.

Easy way to do this is create a new class:

static public class MyConnection {
    static public String ConnectionString { get; set; }
}

Then at the beginning of your code, put

MyConnection.ConnectionString = "whatever your connection string is";

Then anywhere you need the connection string you'd use

OleDbConnection connection = new OleDbConnection(MyConnection.ConnectionString);

Easy way to do this is create a new class:

static public class MyConnection {
    static public String ConnectionString { get; set; }
}

Then at the beginning of your code, put

MyConnection.ConnectionString = "whatever your connection string is";

Then anywhere you need the connection string you'd use

OleDbConnection connection = new OleDbConnection(MyConnection.ConnectionString);

Hi expert, I'm still don't understand the code

MyConnection.ConnectionString = "whatever your connection string is";

is put where. Is put at class or windows form. Hope u can explain detail.Thank you

Somewhere you have a method called Main. Every program has to have one, you put it in that method.

Somewhere you have a method called Main. Every program has to have one, you put it in that method.

You means is every windows form Main. So that I need put it at every windows form 1 times. But I want set a location(database location), and apply to every windows form. So if I change address of database location, I will apply to every Windows Form. So I'm not need to every Windows Form to change location.

You means is every windows form Main. So that I need put it at every windows form 1 times. But I want set a location(database location), and apply to every windows form. So if I change address of database location, I will apply to every Windows Form. So I'm not need to every Windows Form to change location.

or

You mean is put at here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
         summary
         The main entry point for the application.
         summary
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new PDC.AddProduct());
            MyConnection.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;DATA Source=WdsharespaceWansern_SystemWansern_FoamInstyle SofaInstyle_Sofa.mdb;
        }
    }
}

IF I put here will cause

When I try to download your attached file it download as corrupted.
Any you ask how to use the class file to store the database location and change.

class database
    {
        string Con_string = "";
        public string location
        {
            get
            {
                return Con_string;
            }
            set
            {
                Con_string = value;
            }
        }
    }

this is my 1st code which i post to you according to this class file or for any class
First you have create a object of that class to call the method or property, in this class i use property for using this class file and property
create a object of this class like

database obj=new database ();
obj.location="Provider=Microsoft.Jet.OLEDB.4.0;DATA Source=\\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";

here database is the name of class and location is the name of property of class.

use the code tag to post code

When I try to download your attached file it download as corrupted.
Any you ask how to use the class file to store the database location and change.

class database
    {
        string Con_string = "";
        public string location
        {
            get
            {
                return Con_string;
            }
            set
            {
                Con_string = value;
            }
        }
    }

this is my 1st code which i post to you according to this class file or for any class
First you have create a object of that class to call the method or property, in this class i use property for using this class file and property
create a object of this class like

database obj=new database ();
obj.location="Provider=Microsoft.Jet.OLEDB.4.0;DATA Source=\\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";

here database is the name of class and location is the name of property of class.

use the code tag to post code

You can give me your email address.Then I send the file to you. When I see the example will easy to understand what u mean.Thank you very much.You are a kind person

MyConnection.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;DATA Source=WdsharespaceWansern_SystemWansern_FoamInstyle SofaInstyle_Sofa.mdb;

IF I put here will cause

You need to put quote marks (") around the connection string. It also needs to go first in Main, as you have placed it where it will only execute after your program finishes.

@lianpiau
Check Your private message

Sorry still i cant able to check your project because iam using vs 2005 and your code in vs 2008.
anyway try this may be it will help you
1. create a class file code for that

class database
    {
        string Con_string =  "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source=\\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";
        public string location
        {
            get
            {
                return Con_string;
            }
            set
            {
                Con_string = value;
            }
        }
    }

then in your fom

public partial class AddProduct : Form
{
OleDbCommand sCommand;
OleDbDataAdapter sAdapter;
OleDbCommandBuilder sBuilder;
DataSet sDs;
DataTable sTable;
database obj=new database();
string ConnectionString = obj.location;

public AddProduct()
{
InitializeComponent();
}

private void btnAdd_Click(object sender, EventArgs e)
{
OleDbConnection conAuthor;
string conString = obj.location;
conAuthor = new OleDbConnection(conString);

OleDbCommand cmdInsert, cmdCheckID;
OleDbDataReader dtrCheckID;

conAuthor.Open();

string strChechID = "select * from [Product] where Model='" + txtModel.Text + "'and FGrade='" + txtFGrade.Text + "' and Thk='" + txtThk.Text + "'and FSize='" + txtSize.Text + "'";
conString = "Insert Into [Product]([Model],[FGrade], [Thk], [FSize]) Values(?,?,?,?)";

cmdCheckID = new OleDbCommand(strChechID, conAuthor);
cmdInsert = new OleDbCommand(conString, conAuthor);

dtrCheckID = cmdCheckID.ExecuteReader();
if (String.IsNullOrEmpty(txtModel.Text))
{
MessageBox.Show("Model field is empty");
conAuthor.Close();
dtrCheckID.Close();
}
if (String.IsNullOrEmpty(txtFGrade.Text))
{
MessageBox.Show("Foam Grade field is empty");
conAuthor.Close();
dtrCheckID.Close();
}
if (String.IsNullOrEmpty(txtThk.Text))
{
MessageBox.Show("Thick field is empty");
conAuthor.Close();
dtrCheckID.Close();
}
if (String.IsNullOrEmpty(txtSize.Text))
{
MessageBox.Show("Size field is empty");
conAuthor.Close();
dtrCheckID.Close();
}

try
{

if (dtrCheckID.HasRows)
{
MessageBox.Show("Duplicate Data!!!!!!");

dtrCheckID.Close();
}
else
{
dtrCheckID.Close();

cmdInsert.Parameters.AddWithValue("@Model", txtFGrade.Text);
cmdInsert.Parameters.AddWithValue("@FGrade", txtFGrade.Text);
cmdInsert.Parameters.AddWithValue("@Thk", txtThk.Text);
cmdInsert.Parameters.AddWithValue("@FSize", txtSize.Text);


cmdInsert.ExecuteNonQuery();

conAuthor.Close();
txtModel.Text = "";
txtFGrade.Text = "";
txtThk.Text = "";
txtSize.Text = "";
timer1.Enabled = true;
}
}
catch (Exception)
{
//MessageBox.Show("ex.ToString()");
}
}

private void AddProduct_Load(object sender, EventArgs e)
{
string sql = "SELECT * FROM Product ORDER BY Model";
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open();
sCommand = new OleDbCommand(sql, connection);
sAdapter = new OleDbDataAdapter(sCommand);
sBuilder = new OleDbCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "Product");
sTable = sDs.Tables["Product"];
connection.Close();
dataGridView1.DataSource = sDs.Tables["Product"];
dataGridView1.ReadOnly = true;

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}

private void timer1_Tick(object sender, EventArgs e)
{
string sql = "SELECT * FROM Product ORDER BY Model";
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open();
sCommand = new OleDbCommand(sql, connection);
sAdapter = new OleDbDataAdapter(sCommand);
sBuilder = new OleDbCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "Product");
sTable = sDs.Tables["Product"];
connection.Close();
dataGridView1.DataSource = sDs.Tables["Product"];
dataGridView1.ReadOnly = true;

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
timer1.Enabled = false;
}

private void delete_btn_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
sAdapter.Update(sTable);
}
}

private void new_btn_Click(object sender, EventArgs e)
{
dataGridView1.ReadOnly = false;
save_btn.Enabled = true;
new_btn.Enabled = false;
delete_btn.Enabled = false;
btnRefresh.Enabled = false;
btnAdd.Enabled = false;

}

private void save_btn_Click(object sender, EventArgs e)
{
sAdapter.Update(sTable);
dataGridView1.ReadOnly = true;
save_btn.Enabled = false;
new_btn.Enabled = true;
delete_btn.Enabled = true;
btnAdd.Enabled = true;

btnRefresh.Enabled = true;

}

private void btnRefresh_Click(object sender, EventArgs e)
{
string sql = "SELECT * FROM Product ORDER BY Model";
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open();
sCommand = new OleDbCommand(sql, connection);
sAdapter = new OleDbDataAdapter(sCommand);
sBuilder = new OleDbCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "Product");
sTable = sDs.Tables["Product"];
connection.Close();
dataGridView1.DataSource = sDs.Tables["Product"];
dataGridView1.ReadOnly = true;

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

txtFGrade.Text = "";
txtThk.Text = "";
txtSize.Text = "";
}


}

when ever you want to change the location of your database

obj.location="path of your database";

Note : this change will only temporary, that means when ever you terminate your program, change is not effect.


May be it will help you if not mention you problem

You should be looking at writing the database location in the web.config file if its a web project or app.config file if its a windows project.Never hard code it into the class files as its not industry standard and its difficult to manage

Sorry still i cant able to check your project because iam using vs 2005 and your code in vs 2008.
anyway try this may be it will help you
1. create a class file code for that

class database
    {
        string Con_string =  "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source=\\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";
        public string location
        {
            get
            {
                return Con_string;
            }
            set
            {
                Con_string = value;
            }
        }
    }

then in your fom

public partial class AddProduct : Form
{
OleDbCommand sCommand;
OleDbDataAdapter sAdapter;
OleDbCommandBuilder sBuilder;
DataSet sDs;
DataTable sTable;
database obj=new database();
string ConnectionString = obj.location;

public AddProduct()
{
InitializeComponent();
}

private void btnAdd_Click(object sender, EventArgs e)
{
OleDbConnection conAuthor;
string conString = obj.location;
conAuthor = new OleDbConnection(conString);

OleDbCommand cmdInsert, cmdCheckID;
OleDbDataReader dtrCheckID;

conAuthor.Open();

string strChechID = "select * from [Product] where Model='" + txtModel.Text + "'and FGrade='" + txtFGrade.Text + "' and Thk='" + txtThk.Text + "'and FSize='" + txtSize.Text + "'";
conString = "Insert Into [Product]([Model],[FGrade], [Thk], [FSize]) Values(?,?,?,?)";

cmdCheckID = new OleDbCommand(strChechID, conAuthor);
cmdInsert = new OleDbCommand(conString, conAuthor);

dtrCheckID = cmdCheckID.ExecuteReader();
if (String.IsNullOrEmpty(txtModel.Text))
{
MessageBox.Show("Model field is empty");
conAuthor.Close();
dtrCheckID.Close();
}
if (String.IsNullOrEmpty(txtFGrade.Text))
{
MessageBox.Show("Foam Grade field is empty");
conAuthor.Close();
dtrCheckID.Close();
}
if (String.IsNullOrEmpty(txtThk.Text))
{
MessageBox.Show("Thick field is empty");
conAuthor.Close();
dtrCheckID.Close();
}
if (String.IsNullOrEmpty(txtSize.Text))
{
MessageBox.Show("Size field is empty");
conAuthor.Close();
dtrCheckID.Close();
}

try
{

if (dtrCheckID.HasRows)
{
MessageBox.Show("Duplicate Data!!!!!!");

dtrCheckID.Close();
}
else
{
dtrCheckID.Close();

cmdInsert.Parameters.AddWithValue("@Model", txtFGrade.Text);
cmdInsert.Parameters.AddWithValue("@FGrade", txtFGrade.Text);
cmdInsert.Parameters.AddWithValue("@Thk", txtThk.Text);
cmdInsert.Parameters.AddWithValue("@FSize", txtSize.Text);


cmdInsert.ExecuteNonQuery();

conAuthor.Close();
txtModel.Text = "";
txtFGrade.Text = "";
txtThk.Text = "";
txtSize.Text = "";
timer1.Enabled = true;
}
}
catch (Exception)
{
//MessageBox.Show("ex.ToString()");
}
}

private void AddProduct_Load(object sender, EventArgs e)
{
string sql = "SELECT * FROM Product ORDER BY Model";
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open();
sCommand = new OleDbCommand(sql, connection);
sAdapter = new OleDbDataAdapter(sCommand);
sBuilder = new OleDbCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "Product");
sTable = sDs.Tables["Product"];
connection.Close();
dataGridView1.DataSource = sDs.Tables["Product"];
dataGridView1.ReadOnly = true;

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}

private void timer1_Tick(object sender, EventArgs e)
{
string sql = "SELECT * FROM Product ORDER BY Model";
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open();
sCommand = new OleDbCommand(sql, connection);
sAdapter = new OleDbDataAdapter(sCommand);
sBuilder = new OleDbCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "Product");
sTable = sDs.Tables["Product"];
connection.Close();
dataGridView1.DataSource = sDs.Tables["Product"];
dataGridView1.ReadOnly = true;

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
timer1.Enabled = false;
}

private void delete_btn_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
sAdapter.Update(sTable);
}
}

private void new_btn_Click(object sender, EventArgs e)
{
dataGridView1.ReadOnly = false;
save_btn.Enabled = true;
new_btn.Enabled = false;
delete_btn.Enabled = false;
btnRefresh.Enabled = false;
btnAdd.Enabled = false;

}

private void save_btn_Click(object sender, EventArgs e)
{
sAdapter.Update(sTable);
dataGridView1.ReadOnly = true;
save_btn.Enabled = false;
new_btn.Enabled = true;
delete_btn.Enabled = true;
btnAdd.Enabled = true;

btnRefresh.Enabled = true;

}

private void btnRefresh_Click(object sender, EventArgs e)
{
string sql = "SELECT * FROM Product ORDER BY Model";
OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open();
sCommand = new OleDbCommand(sql, connection);
sAdapter = new OleDbDataAdapter(sCommand);
sBuilder = new OleDbCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "Product");
sTable = sDs.Tables["Product"];
connection.Close();
dataGridView1.DataSource = sDs.Tables["Product"];
dataGridView1.ReadOnly = true;

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

txtFGrade.Text = "";
txtThk.Text = "";
txtSize.Text = "";
}


}

when ever you want to change the location of your database

obj.location="path of your database";

Note : this change will only temporary, that means when ever you terminate your program, change is not effect.


May be it will help you if not mention you problem

Anywhere, Thank you for helping so much.

You should be looking at writing the database location in the web.config file if its a web project or app.config file if its a windows project.Never hard code it into the class files as its not industry standard and its difficult to manage

I'm writing windows project in C#. so do u have any code for me to solve problem. Hope you can help me.

Code to use to store database location in app.config file and to use.

1. add app.config file in your project.

<configuration>

    <appSettings>
      <add key="Conn_Str" value="Provider=Microsoft.Jet,..."/>
    </appSettings>

</configuration>

Add this code to your app.config file

to use this in your form use the name space

using System.Configuration;

and the following code

string connstr;
connstr = ConfigurationSettings.AppSettings.Get("Conn_Str");

to change the location

ConfigurationSettings.AppSettings.Set("Conn_Str", "your connection string");

I hope it will help you

Code to use to store database location in app.config file and to use.

1. add app.config file in your project.

<configuration>

    <appSettings>
      <add key="Conn_Str" value="Provider=Microsoft.Jet,..."/>
    </appSettings>

</configuration>

Add this code to your app.config file

to use this in your form use the name space

using System.Configuration;

and the following code

string connstr;
connstr = ConfigurationSettings.AppSettings.Get("Conn_Str");

to change the location

ConfigurationSettings.AppSettings.Set("Conn_Str", "your connection string");

I hope it will help you

Thank you for reply.

Thank you for reply.

I'm got solution already.

coding to app.config file(this for OLEDB access database)
<add name="MyDBConnectionString" providerName="System.Data.OleDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;\\Wdsharespace\Wansern_System\Wansern_Foam\Instyle Sofa\Instyle_Sofa.mdb&quot;"/>

If using SQL
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyDBConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
</connectionStrings>
</configuration>

After that, go to Windows Form code these

using System.Configuration;

ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
string name = conSettings.ProviderName;
string providerName = conSettings.ProviderName;
string connectionString = conSettings.ConnectionString;

go to this address for reference
http://www.ezzylearning.com/tutorial.aspx?tid=8067328

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.