I'm working with a txt files, I must insert that data in mysql database.
So I get that info in a Datatable
, then I can get that data in my gridview
and I must insert that data in database. I can do that, but I have problems with date columns.
For example, When I try to insert, If I have this date in datagridview: 12/01/2018 no problem, but if I have 17/01/2018 not insert, I have manually changed and I've probed it.
this is read txt:
string LogAutoriz_fileName = Server.MapPath("~/Files/") + User.Identity.Name.ToString() + "_" + Path.GetFileName(fup_LogAutoriz_File.PostedFile.FileName);
fup_LogAutoriz_File.SaveAs(LogAutoriz_fileName);
DataTable dt = new DataTable();
List<string[]> list = new List<string[]>();
int maxItem = 0;
using (System.IO.TextReader tr = File.OpenText(LogAutoriz_fileName))
{
string line;
while ((line = tr.ReadLine()) != null)
{
string[] items = line.Trim().Split(' ');
if (maxItem <= items.Count())
{
maxItem = items.Count();
}
list.Add(items);
}
//Crear las columnas del DataTable de Datos
dt.Columns.Add("Comprobante", typeof(string));
dt.Columns.Add("Serie_Comprobante", typeof(string));
dt.Columns.Add("Ruc_Emisor", typeof(string));
dt.Columns.Add("RazonSoc_Emisor", typeof(string));
dt.Columns.Add("Fecha_Emision", typeof(string));
dt.Columns.Add("Fecha_Autoriz", typeof(string));
dt.Columns.Add("Tipo_Emision", typeof(string));
dt.Columns.Add("Ident_Receptor", typeof(string));
dt.Columns.Add("Basura", typeof(string));
dt.Columns.Add("Clave_Acceso", typeof(string));
dt.Columns.Add("Numero_Autor", typeof(string));
dt.Columns.Add("Importe_Total", typeof(string));
foreach (var items in list)
{
dt.Rows.Add(items);
}
//cargar al grid
this.gvwAutorizaciones.DataSource = dt;
this.gvwAutorizaciones.DataBind();
And this is for insert every gridview record in data base:
try
{
using (MySqlConnection sqlCon = new MySqlConnection(conn))
{
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.CommandText = "Insert into autorizaciones " +
"(comprobante, Serie_Comprobante, Ruc_Emisor, RazonSoc_Emisor, " +
"Fecha_Emision, Fecha_Autorizacion, " +
"Tipo_Emision, Ident_Receptor, Clave_Acceso, Numero_Autorizacion) " +
"values(" +
"'" + comprob + "', " +
"'" + serComprob + "', " +
"'" + rucEmisor + "', " +
"'" + razSocEmi + "', " +
"'" + FecEmisor + "', " +
"'" + FecAutoriz + "', " +
"'" + tipoEmision + "', " +
"'" + identRecep + "', " +
"'" + claveAcceso + "', " +
"'" + numAutoriz + "')";
cmd.Connection = sqlCon;
sqlCon.Open();
cmd.ExecuteNonQuery();
sqlCon.Close();
}
}
}
catch (MySqlException ex)
{
}
I call this in a foreach and I can insert records, but for dates, I have that problem.
Is there any way to format the column datatable, for example "yyyy-mm-dd"?
If please anyone can help me,
thanks a lot best regards