Solved

I am working on a small windows forms program with C#

Posted on 2015-02-22
5
56 Views
Last Modified: 2015-02-23
I need to update mysql local server with data from datagridview.
namespace StoreManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'storeDataSet.Customer' table. You can move, or remove it, as needed.
            this.customerTableAdapter.Fill(this.storeDataSet.Customer);

        }

        private void dataGridStudent_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnAdd2Database_Click(object sender, EventArgs e)
        {
                // dataGridStudent.Rows.Count 
                //MessageBox.Show("Count=1");
            SqlConnection storeConnection = new SqlConnection(global::StoreManager.Properties.Settings.Default.StoreConnectionString);
            int last=0;
            string keys = "", values = "";
            for (int i = dataGridStudent.ColumnCount-1; i > 0; i--)
                if (String.Compare(dataGridStudent.Rows[0].Cells[i].Value.ToString(), "") != 0)
                { last = i; break; } // Get the position pf the last non empty cell.(last)
                for (int i = 0; i < dataGridStudent.ColumnCount; i++)
                {

                    if (String.Compare(dataGridStudent.Rows[0].Cells[i].Value.ToString(), "") != 0)
                    {
                        values +="'"+ dataGridStudent.Rows[0].Cells[i].Value.ToString()+"'";
                        keys += dataGridStudent.Columns[i].HeaderText;
                        if (i != last )
                        { keys += ","; values += ","; }
                    }
                }
                try
                {
                   string sqlQuery = "INSERT INTO Customer ("+keys+") values (" + values+ ")";
                    //this.customerBindingSource.AddNew();
                    //MessageBox.Show(sqlQuery);
                   storeConnection.Open();
                   SqlCommand com_query = new SqlCommand(sqlQuery, storeConnection);                    
                   com_query.ExecuteNonQuery();
                   this.customerTableAdapter.Fill(this.storeDataSet.Customer); 
                    
                    MessageBox.Show(sqlQuery);
                    Form2 form2 = new Form2();
                    form2.Show();
                }
                catch (Exception ex)
                {
                   MessageBox.Show(ex.Message);
                }
                finally { //MessageBox.Show("Done!"); 
                    storeConnection.Close();
                    storeDataSet.Customer.AcceptChanges();
                }
        }
    }
}

Open in new window



Can I refresh mysql server with data from datagridview without going about each row with insert commands!?
is there a way to fill database from datagridview just like we fill datagridview from database.

In other words what is the best easiest fastest way to add a row to database using datagridview?
0
Comment
Question by:aboo_s
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40624724
Have a look at using a MVP framework
https://winformsmvp.codeplex.com/
0
 
LVL 10

Author Comment

by:aboo_s
ID: 40625081
Michael74 ,thanks for your response.
I only need to know if in the implementation of sql local server in c# there is a function that will auto fill a sql table from a datagridview. So I don't have to reinvent the wheel. I duckduckgo'ed for an example on the web that will demonstrate the whole local sql <--> datagridview experience but haven't got any good examples. So I thought maybe some expert out there could know anything that will spare the time of going through all documentation of c#.

I do use MVP architecture in my web development ,I use Joomla. But the thing here is that I would like to save time and effort. You know instead of creating views to show tables from sql and creating save functions to save back datagridviews updates to tables, just use the existing functions. Now I have figured out the use of the function to fill a datagridview from a table ,but still have problems with opposite direction!
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40625092
0
 
LVL 10

Author Comment

by:aboo_s
ID: 40625638
Thank you ,that does help a lot ,even though I am using mysql local server rather than ADO ,it still helps a lot.
I haven't yet read the whole article ,the first ,so I am now going to.

Thanks again.
0
 
LVL 10

Author Closing Comment

by:aboo_s
ID: 40625642
Thank you
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

First some basics on Windows 7 Backup.  It has 2 components one is a file based backup which is stored in .zip files each zip is split at around 200 Megabytes and there is the Image Backup which is as the name implies a total image of the partition …
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now