Reputation: 149
I am trying to select then insert a datetime from Table 1 to Table 2. I have successfully insert the data. However, the datetime shown in Table 2 is 0000-00-00 00:00:00. Idk where is the error. Someone please help me with this problem. I am struggling with this. And is this the correct way to SELECT then insert ? (Select from Table 1 then INSERT into Table 2)
try
{
string myConnectionString;
myConnectionString= "server=localhost;uid=root;pwd=root;database=medicloud;SslMode=None;charset=utf8";
MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand cmd = new MySqlCommand();
cmd.CommandType = CommandType.Text;
EncodingProvider ppp;
ppp = CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(ppp);
connection.Open();
string select = "Select time from assign where userId=@name";
cmd.Parameters.AddWithValue("@name", txtValue.Text);
cmd.CommandText = select;
cmd.Connection = connection;
MySqlDataReader selectAssign = cmd.ExecuteReader();
selectAssign.Read();
string assign = (selectAssign["time"].ToString());
selectAssign.Close();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT into bluetooth (userId,arm,armNumberDone,armNumber,comDate,assignDate,status) VALUES (@name, @stupid0, @stupid1, @stupid2, @stupid3, @stupid4, @stupid5)";
cmd.Parameters.AddWithValue("@stupid0", databaseLine);
cmd.Parameters.AddWithValue("@stupid1", counter);
cmd.Parameters.AddWithValue("@stupid2", databaseValue);
cmd.Parameters.AddWithValue("@stupid3", DateTime.Now);
cmd.Parameters.AddWithValue("@stupid4", assign);
cmd.Parameters.AddWithValue("@stupid5", complete);
cmd.Connection = connection;
cmd.ExecuteNonQuery();
connection.Close();
}
catch (MySqlException ex)
{
txtExercise.Text = ex.ToString();
}
Upvotes: 4
Views: 147
Reputation: 127
Please try with this
try
{
string myConnectionString;
myConnectionString = "server=localhost;uid=root;pwd=root;database=medicloud;SslMode=None;charset=utf8";
MySqlConnection connection = new
MySqlConnection(myConnectionString);
MySqlCommand cmd = new MySqlCommand();
cmd.CommandType = CommandType.Text;
EncodingProvider ppp;
ppp = CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(ppp);
connection.Open();
string select = "Select time from assign where userId=@name";
cmd.Parameters.AddWithValue("@name", txtValue.Text);
cmd.CommandText = select;
cmd.Connection = connection;
MySqlDataReader selectAssign = cmd.ExecuteReader();
selectAssign.Read();
string assign = (selectAssign["time"].ToString());
selectAssign.Close();
DateTime assignDate = DateTime.Now;
DateTime.TryParseExact(assign, out assignDate);
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT into bluetooth
(userId,arm,armNumberDone,armNumber,comDate,assignDate,status) VALUES (@name,
@stupid0, @stupid1, @stupid2, @stupid3, @stupid4, @stupid5)";
cmd.Parameters.AddWithValue("@stupid0", databaseLine);
cmd.Parameters.AddWithValue("@stupid1", counter);
cmd.Parameters.AddWithValue("@stupid2", databaseValue);
cmd.Parameters.AddWithValue("@stupid3", DateTime.Now);
cmd.Parameters.AddWithValue("@stupid4", assignDate);
cmd.Parameters.AddWithValue("@stupid5", complete);
cmd.Connection = connection;
cmd.ExecuteNonQuery();
connection.Close();
}
catch (MySqlException ex)
{
txtExercise.Text = ex.ToString();
}
}
Upvotes: 1
Reputation: 3332
You need to use .ExecuteReader()
the use .Read()
to move to each row in the result set. If you are sure the exactly one row will be returned, use .ExecuteScalar()
instead. Research on the difference of both online. Below is an example using .ExecuteReader()
.
I also re-wrote to use using
statements to simplify a bit but not deviate too much from your original code so you do not need to worry about closing and disposing resources since they inherit from IDisposable
and will do that automatically once they exit the using
block:
string assign = DateTime.Now.ToString();
string myConnectionString;
myConnectionString= "server=localhost;uid=root;pwd=root;database=medicloud;SslMode=None;charset=utf8";
string select = "Select time from assign where userId=@name";
using (MySqlConnection con = new MySqlConnection(myConnectionString))
{
using (MySqlCommand cmd = new MySqlCommand(select))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.Parameters.AddWithValue("@name", txtValue.Text);
using (MySqlDataReader cursor = cmd.ExecuteReader())
{
while (cursor.Read())
{
assign = cursor["time"];
}
}
}
string insert = "INSERT into bluetooth (userId,arm,armNumberDone,armNumber,comDate,assignDate,status) VALUES (@name, @stupid0, @stupid1, @stupid2, @stupid3, @stupid4, @stupid5)";
using (MySqlCommand cmd = new MySqlCommand(insert))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.Parameters.AddWithValue("@stupid0", databaseLine);
cmd.Parameters.AddWithValue("@stupid1", counter);
cmd.Parameters.AddWithValue("@stupid2", databaseValue);
cmd.Parameters.AddWithValue("@stupid3", DateTime.Now);
cmd.Parameters.AddWithValue("@stupid4", assign);
cmd.Parameters.AddWithValue("@stupid5", complete);
cmd.ExecuteNonQuery();
}
}
Upvotes: 0
Reputation: 6534
cmd.Parameters.AddWithValue("@stupid3", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
cmd.Parameters.AddWithValue("@stupid4", GetDateString(assign));
Have a method like this:
public static string GetDateString(string date)
{
DateTime theDate;
if (DateTime.TryParseExact(date, "dd/MM/yyyy HH:mm:ss",
CultureInfo.InvariantCulture, DateTimeStyles.None, out theDate))
{
// the string was successfully parsed into theDate
return theDate.ToString("dd/MM/yyyy HH:mm:ss");
}
else
{
// the parsing failed, return some sensible default value
return string.Empty;
}
}
Upvotes: 0