tags:

views:

30

answers:

1

Hey guys, I'm just doin' practise Program to read,write data to Access DB in C# and i'm having a problem in writing data to Access DB though i'm able to read data i.e fetchin' is working fine but when i insert data my "ExecuteNonQuery" is working fine i mean without ne error but when open the Access DB the data is not there.... here is the code what m tryin' to do...


        //// Function For ExecuteNonQuery
        public static bool ExecuteNonQuery(string Query)
        {

            OleDbCommand oledbCommand = new OleDbCommand(Query, connection);
            if (connection.State == ConnectionState.Open)
                connection.Close();
            try
            {
                connection.Open();
                if (oledbCommand.ExecuteNonQuery() > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                connection.Close();
            }
        }

This below code is for Adding Data which gets fired on "Add" Button Press


        private void btnAdd_Click(object sender, EventArgs e)
        {
            simOperator.aim_network_name = txtAimNetNm.Text;
            simOperator.network_id = txtOxiNetID.Text;
            simOperator.network_name = txtNetName.Text;
            simOperator.pack_id = txtPackID.Text;
            simOperator.pack_name = txtPackName.Text;
            SimOperator.Add(simOperator);
            fillText();
        }
        public void fillText()
        {
            txtResult.Text = "";
            SimOperator[] simOperatorList = SimOperator.GetAllOperators();
            foreach (SimOperator sm in simOperatorList)
            {
                txtResult.Text += Program.operator_id + " " + sm.aim_network_name + " " + sm.network_name + " " + sm.network_id + " " + sm.pack_id + " " + sm.pack_name + "\r\n";
            }
        }

Here's the "Add" Function


            string Query = string.Format("insert into {0}({2}) values({1});", calledObject.Name, PropertyValue,PropertyName);
            ExecuteNonQuery(Query);

Actuall SQL query is:

insert into SimOperator(aim_network_name,network_id,network_name,pack_id,pack_name) values('FiveNet','2563','FiveNet-Kurla','1236','5236');

Yeah and My Connection String


static OleDbConnection connection = new OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);

App.config file contains string as

add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\AutoMobileRecharge.mdb;User Id=; Password=;Persist Security Info=True"

The thing m not getting is I have put a Text-Area just beside all the inside fields textboxes so that i can see what is getting inserted, so when insert data that text-area shows the data perfectly but when i open the access db, then theres no data in that, when i close my application and again i ran it then that text-area is empty...this sounds wierd to me.. Any one outthere have faced this kind a problem please help me out here..

+2  A: 

Are you doing it during debug ? in that case look if there's an mdf file inside debug folder and if it contains the data you've just inserted. Vs copy some file to debug folder when you run the app in that mode. If i remember correctly there's an option to tell vs not to copy files when debugging

Stefano
Stefano@ hm, yeah m doin' it in debug mode.. and i hv no idea about that option to tell not copy files when debuggin, nehw i'll check that option and thanx dude, lets see if its works or not.
FosterZ
Stefano@ hey there's a .mdf file inside Debug Folder and it contains the data.. Can you explain me why this happens i mean creating another mdf file in debug folder ?
FosterZ
@FosterZ i suppose it happens to not corrupt data during development. honestly i don't remember how to avoid coping the file, try select the mdf file inside your project (i suppose you have added it to the project) , select the properties tab and (if it's present ) change the "copy local" option to false
Stefano