C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,475 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,752 people online right now. Registration is fast and FREE... Join Now!




C sharp and database

 

C sharp and database, with MS Access ,not add new records

programmer2u

3 Jul, 2009 - 12:57 PM
Post #1

New D.I.C Head
*

Joined: 1 May, 2009
Posts: 8

[b]hi PLEASE SOMEONE TELL ME WHY THIS CODE NOT WORKING
AS THERE WERE NO ERRORS , BUT WHEN I ADD NEW RECORDS IT NOT INSERT IN DATABASE





CODE

void Button6Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbDataAdapter da;
        
            
            
            
            string connect="Provider=Microsoft.jet.oledb.4.0;data source=.\\db1.mdb";
          toolStripProgressBar1.Value=25;
          OleDbConnection con = new OleDbConnection(connect);
          con.Open();
          DataSet ds1;
          ds1=new DataSet();
          //ds1.Tables.Add("r");
          toolStripProgressBar1.Value=25;
          
          string sql="select * from red";
          da= new System.Data.OleDb.OleDbDataAdapter(sql,con);
          
           System.Data.OleDb.OleDbCommandBuilder cb;
cb = new System.Data.OleDb.OleDbCommandBuilder( da );
          
          da.Fill(ds1,"red");
          
          int MaxRows = 0;
int inc = 0;
        MaxRows = ds1.Tables["red"].Rows.Count;  
          
          
          
          //To add a record to the Dataset, you need to create a new Row
          DataRow dr=ds1.Tables["red"].NewRow();
          
          MaxRows = MaxRows + 1;
inc = MaxRows - 1;
MessageBox.Show(textBox2.Text);
          
          //But the Row will not have any data.
          //To add data to the row, the format is this:
          dr[1]=textBox2.Text.ToString();
          dr[2]=textBox3.Text.ToString();
          
          toolStripProgressBar1.Value=75;
          
          
          //Finally, you issue the Add command:
          ds1.Tables["red"].Rows.Add(dr);
          ds1.AcceptChanges();

          
          //The only thing you need to do is tell it which Dataset holds
          //all the records, and its name:

             //da.Update( ds1, "Workers" );
          
          da.Update(ds1,"red");
          
          toolStripProgressBar1.Value=100;
          con.Close();
        }
    }
}


Mod Edit: Please use code tags when posting your code. Code tags are used like so => code.gif

Thanks,
PsychoCoder smile.gif

This post has been edited by PsychoCoder: 3 Jul, 2009 - 03:19 PM

User is offlineProfile CardPM
+Quote Post


fixo

RE: C Sharp And Database

5 Jul, 2009 - 12:32 AM
Post #2

D.I.C Head
**

Joined: 10 May, 2009
Posts: 78



Thanked: 5 times
My Contributions
QUOTE(programmer2u @ 3 Jul, 2009 - 12:57 PM) *

[b]hi PLEASE SOMEONE TELL ME WHY THIS CODE NOT WORKING
AS THERE WERE NO ERRORS , BUT WHEN I ADD NEW RECORDS IT NOT INSERT IN DATABASE





CODE

void Button6Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbDataAdapter da;
        
            
            
            
            string connect="Provider=Microsoft.jet.oledb.4.0;data source=.\\db1.mdb";
          toolStripProgressBar1.Value=25;
          OleDbConnection con = new OleDbConnection(connect);
          con.Open();
          DataSet ds1;
          ds1=new DataSet();
          //ds1.Tables.Add("r");
          toolStripProgressBar1.Value=25;
          
          string sql="select * from red";
          da= new System.Data.OleDb.OleDbDataAdapter(sql,con);
          
           System.Data.OleDb.OleDbCommandBuilder cb;
cb = new System.Data.OleDb.OleDbCommandBuilder( da );
          
          da.Fill(ds1,"red");
          
          int MaxRows = 0;
int inc = 0;
        MaxRows = ds1.Tables["red"].Rows.Count;  
          
          
          
          //To add a record to the Dataset, you need to create a new Row
          DataRow dr=ds1.Tables["red"].NewRow();
          
          MaxRows = MaxRows + 1;
inc = MaxRows - 1;
MessageBox.Show(textBox2.Text);
          
          //But the Row will not have any data.
          //To add data to the row, the format is this:
          dr[1]=textBox2.Text.ToString();
          dr[2]=textBox3.Text.ToString();
          
          toolStripProgressBar1.Value=75;
          
          
          //Finally, you issue the Add command:
          ds1.Tables["red"].Rows.Add(dr);
          ds1.AcceptChanges();

          
          //The only thing you need to do is tell it which Dataset holds
          //all the records, and its name:

             //da.Update( ds1, "Workers" );
          
          da.Update(ds1,"red");
          
          toolStripProgressBar1.Value=100;
          con.Close();
        }
    }
}


Mod Edit: Please use code tags when posting your code. Code tags are used like so => code.gif

Thanks,
PsychoCoder smile.gif


This one should work for you
And also, think you need some ADO.NET basic
Take a look at MSDN library about Insert, Update
and Delete commands
From other side using of Try...Catch statment is
would be a good practic for you

Change column names to your suit

CODE

        private void button6_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            try
            {
                System.Data.OleDb.OleDbDataAdapter da;


                string connect = "Provider=Microsoft.jet.oledb.4.0;data source=.\\db1.mdb";
                //toolStripProgressBar1.Value = 25;
                con.ConnectionString = connect;
                this.Cursor = Cursors.WaitCursor;
                con.Open();
                DataSet ds1;
                ds1 = new DataSet();
                //ds1.Tables.Add("r");
                //toolStripProgressBar1.Value = 25;

                string sql = "select * from red";
                da = new System.Data.OleDb.OleDbDataAdapter(sql, con);

                System.Data.OleDb.OleDbCommandBuilder cb;
                cb = new System.Data.OleDb.OleDbCommandBuilder(da);

                da.Fill(ds1, "red");

                //int MaxRows = 0;
                //int inc = 0;
                //MaxRows = ds1.Tables["red"].Rows.Count;



                //To add a record to the Dataset, you need to create a new Row
                DataRow dr = ds1.Tables["red"].NewRow();

                //MaxRows = MaxRows + 1;
                //inc = MaxRows - 1;
                //MessageBox.Show(textBox2.Text);

                //But the Row will not have any data.
                //To add data to the row, the format is this:
                dr["Column1"] = textBox2.Text.ToString();
                dr["Column2"] = textBox3.Text.ToString();

                //toolStripProgressBar1.Value = 75;


                //Finally, you issue the Add command:
                ds1.Tables["red"].Rows.Add(dr);
                //ds1.AcceptChanges();
                // create insert sql string
                sql = string.Format("insert into red " +
                    "(Column1, Column2) values " +
                "('{0}','{1}')", textBox2.Text, textBox3.Text);
                //create insert command, set properties
                da.InsertCommand = new OleDbCommand(sql);
                da.InsertCommand.Connection = con;

                //The only thing you need to do is tell it which Dataset holds
                //all the records, and its name:

                //da.Update( ds1, "Workers" );

                da.Update(ds1.Tables["red"]);

                //toolStripProgressBar1.Value = 100;

            }
            catch (OleDbException oex)
            {
                MessageBox.Show(oex.Message + "\n" + oex.StackTrace);
            }
            finally
            {
                con.Close();
            }
            this.Cursor = Cursors.Default;
        }

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:16AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month