Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am running my project in VS2008 and give datas to insert to table.

In current program, datas are added and when the program selects from that table, I see changes.
When rerunning the program after changing a little unrelated codes or close VS2008 and Open a new and run program again, that data does not appear to be added (but when I add data in server explorer of VS2008, without running the program, the data is added).
in class #1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Mallmanagement
{
    public class mydataaccess
    {
        public string ServerName;
        public string DBName = "goldDB.mdf";
        public string username;
        public string password;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        public mydataaccess()
        {
            con = new SqlConnection();
            cmd = new SqlCommand();
            da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
        }
        public void connect()
        {
            string cs = "Data source=.\\SQLEXPRESS;Attachdbfilename=|DataDirectory|\\{0};Integrated security=true;user Instance=true";
            cs = string.Format(cs, this.DBName);
            con.ConnectionString = cs;
            con.Open();
        }
        public void Disconnect()
        {
            con.Close();
        }
        public DataTable select(string sql)
        {
            DataTable dt = new DataTable();
            cmd.CommandText = sql;
            da.Fill(dt);
            return dt;
        }
        public void docommand(string sql)
        {
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
        }
    }
}

and class #2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Mallmanagement
{
    class sanadlogic
    {
        mydataaccess md = new mydataaccess();
        public int codehesab;
        public int codesanad;
        public int day;
        public int month;
        public int year;
        public DataTable select()
        {
            md.connect();
            string ss = "Select * from sanadha";
            DataTable dt = new DataTable();
            dt = md.select(ss);
            md.Disconnect();
            return dt;
        }
        public void Insert()
        {
            md.connect();
            string AA = "Insert into sanadha(codesanad,codehesab,day,month,year)values({0},{1},{2},{3},{4}";
            AA = string.Format(AA, this.codesanad, this.codehesab, this.day, this.month, this.year);
            md.docommand(AA);
            md.Disconnect();
        }
    }
}


What is the problem?
Can I to change, add, delete data in running program?
And can I change, add, delete data in tables?
Posted
Updated 31-Mar-11 5:50am
v2
Comments
[no name] 31-Mar-11 11:42am    
Any error it shows. Have you tried it by exceptions or not?
Dalek Dave 31-Mar-11 11:51am    
Edited for Grammar, Spelling and Readability.
BobJanova 31-Mar-11 12:15pm    
Do you need to commit a transaction? That depends on how your database is set up but that would result in this type of problem.

1 solution

If I understood you problem correctly, one cause could be that you're attaching the database file for example from your bin directory and it's copied into that directory from your project directory during build. This would cause the overwrite of the db file every time you build the solution.

Side-note: Why use separate fields for year/month/date. Consider using date datatype, which I believe would save you from lot's of trouble later.
 
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