Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone.

I have an excel sheet, sheet1, sheet2 and sheet3

I have a datareader that works to read and load excel files and sheets in to my datagridview, however

I want to save different sheet from different table on my database

How to do this? can any one help me to do this?

Thank's in advance

What I have tried:

C#
private void btnSaved_Click(object sender, EventArgs e)
        {
            int qtr = 0;
            string att_qtr = "ATTENDANCE";

            MySqlConnection con = new MySqlConnection("Server = DESKTOP-9H7QBOH; Database = sti_spms; UID = root; Password = 1234;");
                try
                {
                    string query = "INSERT INTO tbl_secondsem_grades(STUDENT_NO, NAME, SUBJECT, SECTION, GRADE, INITIAL_GRADE, QTR)" + "Values(@STUDENT_NO, @NAME, @SUBJECT, @SECTION, @GRADE, @INITIAL_GRADE, @QTR)";
                    query +="INSERT INTO tbl_attendance(STUDENT_ID, NAME, SUBJECT, SECTION, TOTAL_ABSENCES)" + "Values(@STUDENT_ID, @NAME, @SUBJECT, @SECTION, @TOTAL_ABSENCES)";
                    MySqlCommand cmd = new MySqlCommand(query, con);
                    DataTable dt = new DataTable();
                    con.Open();
                    for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
                    {
                       
                        if(quarter.Equals("1st QTR"))
                        {
                            qtr = 1;
                        }
                        else if(quarter.Equals("2nd QTR"))
                        {
                            qtr = 2;
                        }
                        else if(quarter.Equals("3rd QTR"))
                        {
                            qtr = 3;
                        }
                        else if(quarter.Equals("4th QTR"))
                        {
                            qtr = 4;
                        }
                        else if (quarter.Equals("ATTENDANCE"))
                        {
                            att_qtr = "ATTENDANCE";
                        }
                        else
                        {
                            MessageBox.Show("Error!");
                        }

                        //int num = Convert.ToInt32(dataGridView1.Rows[i].Cells["STUDENT NO"].Value.ToString());
                        cmd.Parameters.AddWithValue("@STUDENT_NO", dataGridView1.Rows[i].Cells["STUDENT NO"].Value.ToString());
                        cmd.Parameters.AddWithValue("@NAME", dataGridView1.Rows[i].Cells["NAME"].Value.ToString());
                        cmd.Parameters.AddWithValue("@SUBJECT", dataGridView1.Rows[i].Cells["SUBJECT"].Value.ToString());
                        cmd.Parameters.AddWithValue("@SECTION", dataGridView1.Rows[i].Cells["SECTION"].Value.ToString());
                        cmd.Parameters.AddWithValue("@INITIAL_GRADE", dataGridView1.Rows[i].Cells["INITIAL GRADE"].Value.ToString());
                        cmd.Parameters.AddWithValue("@GRADE", dataGridView1.Rows[i].Cells["QG"].Value.ToString());
                        cmd.Parameters.AddWithValue("@QTR", qtr);
                        cmd.Parameters.AddWithValue("@STUDENT_ID", dataGridView1.Rows[i].Cells["STUDENT ID"].Value.ToString());
                        cmd.Parameters.AddWithValue("@NAME", dataGridView1.Rows[i].Cells["NAME"].Value.ToString());
                        cmd.Parameters.AddWithValue("@SUBJECT", dataGridView1.Rows[i].Cells["SUBJECT"].Value.ToString());
                        cmd.Parameters.AddWithValue("@SECTION", dataGridView1.Rows[i].Cells["SECTION"].Value.ToString());
                        cmd.Parameters.AddWithValue("@Total_Absences", dataGridView1.Rows[i].Cells["Total_Absences"].Value.ToString());
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Data  Sucessfully Saved!");
                }
    
        }
Posted
Updated 21-Sep-17 4:06am
Comments
Richard MacCutchan 20-Sep-17 12:52pm    
What is the problem with your code?
Member 13396840 20-Sep-17 14:22pm    
can't save the data from excel sheet (Attendance) to tbl_attendance sir it says the STUDENT_NO already define, so i tried to change the it to STUDENT_ID still it says STUDENT_ID already define
Richard MacCutchan 20-Sep-17 15:22pm    
Presumably your STUDENT_ID field is an auto identity.
Member 13396840 21-Sep-17 3:45am    
i don't know what to do sir
Richard MacCutchan 21-Sep-17 4:14am    
Neither do I, because I have no real idea what your system is doing. If you get an error then you need to look at your code to see why it occurs. All you have told us is that the error you see is "it says the STUDENT_NO already define" which really tells us nothing. What is the definition of STUDENT_NO in your database, and where are you trying to define it a second time?

1 solution

C#
catch (Exception ex)
                {
                    MessageBox.Show("Data  Sucessfully Saved!");
                }

That is just beyond ridiculous.
 
Share this answer
 
Comments
Member 13396840 21-Sep-17 10:13am    
wrong code sir?
Richard MacCutchan 21-Sep-17 10:15am    
Why are you putting out a success message when you catch an exception? Do you understand what exceptions are?
Member 13396840 21-Sep-17 10:20am    
i'm sorry sir i'm working on ive changed it in MessageBox.Show(ex.Message);

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