Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to update from Datagridview to database.i did with following codings.but it is giving error.how can i solve this prb.pls help me

for (int i = 0; i <= count-1; i++)
            {              
                              
                fname = dataGridView1.Rows[i].Cells["Fees_name"].Value.ToString();
                amt = dataGridView1.Rows[i].Cells["Amount"].Value.ToString();
                string qu = "insert into feesstudcture(Fees_name,Amount)values('" + fname + "','" + amt + "')";
                string constring = "data source=localhost;database=newschool;userid=root;password=";
                MySqlConnection con = new MySqlConnection(constring);
                con.Open();
                MySqlCommand cmd = new MySqlCommand(qu, con);
                cmd.ExecuteNonQuery();
               
            }
            MessageBox.Show("succes");
Posted
Updated 30-May-11 22:58pm
v2
Comments
ambarishtv 31-May-11 5:00am    
specify that error.
Steven.Pinto2000 31-May-11 5:00am    
what is the error you are getting

You can try this...

for (int i = 0; i &lt;= dataGridView1.Rows.Count-1; i++)
{

fname = dataGridView1.Rows[i].Cells["Fees_name"].Value.ToString();
amt = dataGridView1.Rows[i].Cells["Amount"].Value.ToString();
string qu = "insert into feesstudcture(Fees_name,Amount)values('" + fname + "'," + amt + ")";
string constring = "data source=localhost;database=newschool;userid=root;password=";
MySqlConnection con = new MySqlConnection(constring);
con.Open();
MySqlCommand cmd = new MySqlCommand(qu, con);
cmd.ExecuteNonQuery();

}
MessageBox.Show("succes");
 
Share this answer
 
v2
Comments
Rojeh 1-Jun-11 3:52am    
my 5 for "dataGridView1.Rows.Count-1"
i advice using it in this case
Just try.... Not sure

Change loop to for(int i=0;i<count-1>;i++)

or

Change Cells["name"] to Cells[indexvalue] Eg: Cells[0]

or

Insert "Integrated Security=false" in connection string

Hope it will help you
 
Share this answer
 
v4
i dont think one can be of much help if one doesnt know what error u r getting.

But just by looking at it. what datatype id ur amount field in teh table.. i see ur trying to insert itas a string!

IS ur connection string in correct form?
 
Share this answer
 
SURE the error is from the variable count
if count is intialized with value < 1 then the loop will not be entered
and if initialized with value >= 1 then the loop will be entered and it will not be left since the count is not changed inside the loop (infinite loop).
so in both cases you will not reach your target!!!
 
Share this answer
 
v2
Hi ngnagarajan,

Please we need the error that faced you in order finding a solution for your problem .
 
Share this answer
 
There are lots of possible reasons:
1) count could be wrong: this would probably cause an "array index out of range" error.
2) The data in your DataGridView might be in the wrong format for the cells.
3) You might need a password on your DB.
4) The name of the database could be wrong.
5) You may already have identical data in your database.
6) Your database may require other information in order to do the insert.
7) You could be trying to compile this as VB.
8) Other.

Without the actual error message, it is impossible to say.
However, do not do it that way anyway: you leave yourself open to an accidental or deliberate SQL Injection attack. Use Parametrized queries instead:
string qu = "INSERT INTO feesstudcture (Fees_name, Amount) VALUES (@FEN, @AMT)";
string constring = "data source=localhost;database=newschool;userid=root;password=";
MySqlConnection con = new MySqlConnection(constring);
con.Open();
MySqlCommand cmd = new MySqlCommand(qu, con);
cmd.Parameters.AddWithValue("@FEN", dataGridView1.Rows[i].Cells["Fees_name"].Value);
cmd.Parameters.AddWithValue("@AMT", dataGridView1.Rows[i].Cells["Amount"].Value);
cmd.ExecuteNonQuery();
Oh, and move the connection outside the loop - you do not need to create a new one each time you go round!
 
Share this answer
 
v2

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