thank you. i did like below. i detached the database then add to my project under db folder . i use the following code
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace winform
{
public partial class Form1 : Form
{
SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["testdbConnectionString"].ConnectionString);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sqlrooms;
SqlCommand addroomcmd=new SqlCommand();
sqlrooms = "insert into testtbl(fname,lname)";
sqlrooms += string.Format("values(@fname,@lname)");
addroomcmd = new SqlCommand(sqlrooms, dbcon);
addroomcmd.CommandType = CommandType.Text;
addroomcmd.Parameters.AddWithValue("@fname", (string)textBox1.Text);
addroomcmd.Parameters.AddWithValue("@lname", (string)textBox2.Text);
dbcon.Open();
addroomcmd.ExecuteNonQuery();
dbcon.Close();
MessageBox.Show("added successfully");
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void listnames()
{
dataGridView2.DataMember = "testtbl";
dataGridView2.Columns[0].HeaderText="ID";
dataGridView2.Columns[1].HeaderText = "Fname";
dataGridView2.Columns[2].HeaderText = "Lastname";
}
private void button2_Click(object sender, EventArgs e)
{
string sqlfloor;
sqlfloor = "select idno, fname,lname from testtbl";
SqlDataAdapter adapter = new SqlDataAdapter(sqlfloor, dbcon);
dbcon.Open();
DataSet ds = new DataSet();
adapter.Fill(ds, "testtbl");
dataGridView2.DataSource = ds;
this.listnames();
dbcon.Close();
}
}
}
the app.config is before detached the databse it was like this
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="testdbConnectionString" connectionString="server=localhost;user id=sa;password=12345;database=testdb"
providerName="System.Data.sqlclient" />
</connectionStrings>
</configuration>
upto this everything is work fine..in my computer. now i want to take the project into other clients so i detached the database and then change my app.config connection like this one
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="winform.Properties.Settings.testdbConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db\testdb.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
now it gives me error
NullReference Exception was unhandle
Object reference not set to an instance of an object.
use the "New" keyword to create an object instant
please help.. thanks