Looking for some help for writing array values into one of my tables within my sql database. The array has already calculated all the values, and I want to insert these values into my database. However when I run the code I'm getting an error message stating "An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
Additional information: Parameter '@PE' has already been defined."
It says the same for each parameter if I remove the one before it.
I'm not quite sure whats going on as I've used this code in another form for writing stuff into a database and that works fine! Any help would be great.
My code is,
try
{
string myconnection = "datasource=localhost;port=3306;username=root;password=1234";
MySqlConnection myconn = new MySqlConnection(myconnection);
MySqlDataAdapter MyDataAdaptor = new MySqlDataAdapter();
MyDataAdaptor.SelectCommand = new MySqlCommand("select * lossdatabase.forecasttable;", myconn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(MyDataAdaptor);
myconn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
string constring = "datasource=localhost;port=3306;username=root;password=1234; ";
string query = " insert into lossdatabase.forecasttable (PE, Production_Time, Potential) VALUES(@PE, @Production_Time, @Potential;";
MySqlConnection conLossDB = new MySqlConnection(constring);
MySqlCommand cmdLossDB = new MySqlCommand(query, conLossDB);
for (int i=0; i<366; i++)
{
cmdLossDB.Parameters.AddWithValue("@PE", textBox2.Text);
cmdLossDB.Parameters.AddWithValue("@Production_Time", forecast[i,2]);
cmdLossDB.Parameters.AddWithValue("@Potential", forecast[i,3]);
}
MySqlDataReader myReader;
try
{
conLossDB.Open();
myReader = cmdLossDB.ExecuteReader();
MessageBox.Show("Forecast Results Saved");
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}