How to remove empty rows from DataTable - c#

I am working on importing data from an Excel sheet to database. The Excel sheet contains few empty rows and I want to remove those empty rows, then insert cleared data into database.
I have written a code by referring other code, this is the code for inserting values:
OleDbConnection cnn = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtExcelFile.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'");
//DataTable dt = new DataTable();
try
{
cnn.Open();
OleDbDataAdapter data = new OleDbDataAdapter("select * from [Customers$]", cnn);
data.Fill(dsExcel);
dgvCustomers.ColumnHeadersVisible = false;
SqlConnection connection = new SqlConnection("Data Source=COMPUTER-8EB749;Initial Catalog=KITS;Integrated Security=true");
connection.Open();
for (int i = 0; i < dsExcel.Tables[0].Rows.Count; i++)
{
string ID = ds.Tables[0].Rows[i][0].ToString();
Int16 CustID = Convert.ToInt16(ID);
string CustName = dsExcel.Tables[0].Rows[i][1].ToString();
string CardScheme = dsExcel.Tables[0].Rows[i][2].ToString();
string Outlet = dsExcel.Tables[0].Rows[i][3].ToString();
string TerminalNum = dsExcel.Tables[0].Rows[i][4].ToString();
Int32 Terminal = Convert.ToInt32(TerminalNum);
string Date1 = dsExcel.Tables[0].Rows[i][5].ToString();
DateTime Date = Convert.ToDateTime(Date1);
string Time = dsExcel.Tables[0].Rows[i][6].ToString();
DateTime DateTime = Convert.ToDateTime(Time);
string Amount1 = ds.Tables[0].Rows[i][7].ToString();
double Amount = Convert.ToDouble(Amount1);
SqlCommand com = new SqlCommand("insert into Customer(CustID,CustName,CardScheme,Outlet,TerminalNum,TranDate,TranDateTime,Amount) values ('" + CustID + "','" + CustName + "','" + CardScheme + "','" + Outlet + "','" + Terminal + "','" + Date + "','" + DateTime + "','" + Amount + "')", connection);
com.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("Data Inserted Successfully.");
}
Can anyone say me how can I remove empty rows so that i can insert only data?!

This will remove all rows that which each of it's columns contain either nothing or white space:
dataTable = dataTable.Rows
.Cast<DataRow>()
.Where(row => !row.ItemArray.All(field => field is DBNull ||
string.IsNullOrWhiteSpace(field as string)))
.CopyToDataTable();

Try this.
public bool InsertRowsToDataBase()
{
try
{
DataTable excelTable = new DataTable();
string connString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtExcelFile.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
using (OleDbConnection cnn = new OleDbConnection(connString))
{
string query = "select * from [Customers$]";
using (OleDbDataAdapter data = new OleDbDataAdapter(query, cnn))
{
data.Fill(excelTable);
}
}
dgvCustomers.ColumnHeadersVisible = false;
connString = "Data Source=COMPUTER-8EB749;Initial Catalog=KITS;Integrated Security=true";
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
for (int i = 0; i < excelTable.Rows.Length; i++)
{
//takes from the 3rd row
if (i > 1)
{
DataRow row = excelTable.Rows[i];
object ID = row[0];
if (ID != null && !String.IsNullOrEmpty(ID.ToString().Trim()))
{
Int16 CustID = Convert.ToInt16(ID);
string CustName = row[1].ToString();
string CardScheme = row[2].ToString();
string Outlet = row[3].ToString();
string TerminalNum = row[4].ToString();
Int32 Terminal = Convert.ToInt32(TerminalNum);
string Date1 = row[5].ToString();
DateTime Date = Convert.ToDateTime(Date1);
string Time = row[6].ToString();
DateTime DateTime = Convert.ToDateTime(Time);
string Amount1 = row[7].ToString();
double Amount = Convert.ToDouble(Amount1);
string columnNames = "CustID,CustName,CardScheme,Outlet,TerminalNum,TranDate,TranDateTime,Amount";
string query = String.Format("insert into Customer(0}) values ('{1}', '{2}','{3}','{4}','{5}','{6}','{7}','{8}')",
columnNames, CustID, CustName, CardScheme, Outlet, Terminal, Date, DateTime, Amount);
using (SqlCommand com = new SqlCommand(query, connection))
{
com.ExecuteNonQuery();
}
}
}
//this is your last row. do whatever you want with this
DataRow lastRow = excelTable.Rows[excelTable.Rows.Count - 1];
}
}
return true;
}
catch (Exception exception)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
return false;
}
}
Please note that I am just checking if ID is null and not inserting any such rows as ID will be the PK in your table.

This will remove all empty rows from the data table:
DataTable dt = dt.Rows
.Cast<DataRow>()
.Where(row => !row.ItemArray.All(f => f is DBNull))
.CopyToDataTable();
OR
DataTable dt = dt.Rows
.Cast<DataRow>()
.Where(row => !row.ItemArray.All(f => f is DBNull ||
string.IsNullOrEmpty(f as string ?? f.ToString())))
.CopyToDataTable();

try
{
OpenOleDBConnection();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter("select * from [" + SelectedSheet + "]", Connection);
dataAdapter.Fill(DataTable);
if ((DataTable != null) && (DataTable.Rows != null) && (DataTable.Rows.Count > 0))
{
List<System.Data.DataRow> removeRowIndex = new List<System.Data.DataRow>();
int RowCounter = 0;
foreach (System.Data.DataRow dRow in DataTable.Rows)
{
for(int index = 0; index < DataTable.Columns.Count; index++)
{
if (dRow[index] == DBNull.Value)
{
removeRowIndex.Add(dRow);
break;
}
else if (string.IsNullOrEmpty(dRow[index].ToString().Trim()))
{
removeRowIndex.Add(dRow);
break;
}
}
RowCounter++;
}
// Remove all blank of in-valid rows
foreach (System.Data.DataRow rowIndex in removeRowIndex)
{
DataTable.Rows.Remove(rowIndex);
}
}
}
catch(Exception e)
{
WPFMessageBox.Show(e.Message, Globalization.GetValue("Import_ImportOption_FormHeader"), WPFMessageBoxButtons.OK, WPFMessageBoxImage.Error);
}
finally
{
CloseOleDBConnection();
}
Here I m also skipping the rows if they have blank entry in any of the row.

I've made this private method that does the trick.
It takes a DataTable as argument and returns the same DataTable without empty rows.
private DataTable StripEmptyRows(DataTable dt)
{
List<int> rowIndexesToBeDeleted = new List<int>();
int indexCount = 0;
foreach(var row in dt.Rows)
{
var r = (DataRow)row;
int emptyCount = 0;
int itemArrayCount = r.ItemArray.Length;
foreach(var i in r.ItemArray) if(string.IsNullOrWhiteSpace (i.ToString())) emptyCount++;
if(emptyCount == itemArrayCount) rowIndexesToBeDeleted.Add(indexCount);
indexCount++;
}
int count = 0;
foreach(var i in rowIndexesToBeDeleted)
{
dt.Rows.RemoveAt(i-count);
count++;
}
return dt;
}

To check Empty Rows
Foreach(DataRow as row in datable.Rows) {
var isEmpty = row.ItemArray.All(c => c is DBNull);
if(!isEmpty) {
//Your Logic
}
}

Why not simply ignore empty rows directly before you are inserting them?
if(string.IsNullOrEmpty(ID + CustName + CardScheme /*.. and so on */))
{
continue;
}
Like this:
for (int i = 0; i < dsExcel.Tables[0].Rows.Count; i++)
{
string ID = ds.Tables[0].Rows[i][0].ToString();
Int16 CustID = Convert.ToInt16(ID);
string CustName = dsExcel.Tables[0].Rows[i][1].ToString();
string CardScheme = dsExcel.Tables[0].Rows[i][2].ToString();
string Outlet = dsExcel.Tables[0].Rows[i][3].ToString();
string TerminalNum = dsExcel.Tables[0].Rows[i][4].ToString();
Int32 Terminal = Convert.ToInt32(TerminalNum);
string Date1 = dsExcel.Tables[0].Rows[i][5].ToString();
DateTime Date = Convert.ToDateTime(Date1);
string Time = dsExcel.Tables[0].Rows[i][6].ToString();
DateTime DateTime = Convert.ToDateTime(Time);
string Amount1 = ds.Tables[0].Rows[i][7].ToString();
double Amount = Convert.ToDouble(Amount1);
/*** Add this if-statement to you code! ***/
if(string.IsNullOrEmpty(ID + CustName + CardScheme + Outlet + TerminalNum + Date1 + Time + Amount1))
{
continue;
}
SqlCommand com = new SqlCommand("insert into Customer(CustID,CustName,CardScheme,Outlet,TerminalNum,TranDate,TranDateTime,Amount) values ('" + CustID + "','" + CustName + "','" + CardScheme + "','" + Outlet + "','" + Terminal + "','" + Date + "','" + DateTime + "','" + Amount + "')", connection);
com.ExecuteNonQuery();
}

I modified Cfrim's answer. You need to check for both empty and whitespace strings. The white space comes from the deleted cells and the empty space comes from deleted data.
private DataTable StripEmptyRows(DataTable dt)
{
List<int> rowIndexesToBeDeleted = new List<int>();
int indexCount = 0;
foreach(var row in dt.Rows)
{
var r = (DataRow)row;
int emptyCount = 0;
int itemArrayCount = r.ItemArray.Length;
foreach (var i in dr.ItemArray)
{
if (string.IsNullOrEmpty(i.ToString()) || string.IsNullOrWhiteSpace(i.ToString()))
emptyCount++;
}
if(emptyCount == itemArrayCount) rowIndexesToBeDeleted.Add(indexCount);
indexCount++;
}
int count = 0;
foreach(var i in rowIndexesToBeDeleted)
{
dt.Rows.RemoveAt(i-count);
count++;
}
return dt;
}

for (int i = dt.Rows.Count - 1; i >= 0; i--) {
if (dt.Rows[i][1] == DBNull.Value) {
dt.Rows[i].Delete();
}
}
dt.AcceptChanges();
return dt;

Your DB itself has empty rows?? Thats quite strange. May be filter it while you do a select query by saying a primary key column is not NULL

public static DataTable RemoveEmptyRows(DataTable dt)
{
List removeRowIndex = new List();
foreach (DataRow dRow in dt.Rows)
{
for (int index = 0; index < dt.Columns.Count; index++)
{
if (string.IsNullOrEmpty(dRow[index].ToString().Trim()))
{
removeRowIndex.Add(dRow);
break;
}
else if (dRow[index] == DBNull.Value)
{
removeRowIndex.Add(dRow);
break;
}
}
}
foreach (DataRow rowIndex in removeRowIndex)
{
dt.Rows.Remove(rowIndex);
}
return dt;
}

this works perfect for me:
dt.Load(cmd.ExecuteReader());
var x = dt.Rows.Cast<DataRow>()
.Where(row => !Array.TrueForAll(row.ItemArray, value =>
{ return value.ToString().Length == 0; }
));
dt = x.CopyToDataTable();

I change a little in #Levitikon post https://stackoverflow.com/a/9233696/5848472
with #shA.t comment , and this code remove all empty Rows and Columns in datatable:
dt = ds.Tables[tablename].Rows
.Cast<DataRow>()
.Where(row => !row.ItemArray.All(field => field is DBNull ||
string.IsNullOrWhiteSpace(field as string ?? field.ToString())))
.CopyToDataTable();
foreach (var column in dt.Columns.Cast<DataColumn>().ToArray())
{
if (dt.AsEnumerable().All(dr => dr.IsNull(column)))
dt.Columns.Remove(column);
}

This worked for me. If we do not check rows and directly do CopyToDataTable() then you may get an exception when the data table has empty rows.
var rows = tbl.Rows.Cast<DataRow>()
.Where(row => !row.ItemArray.All(field => field is DBNull || String.IsNullOrWhiteSpace(field as string ?? field.ToString())));
if (rows.Any())
tbl = rows.CopyToDataTable();

Based on existing answers I use following
public static bool AllColumnsEmpty(this DataRow row)
{
if (row == null)
{
return true;
}
else
{
foreach (var value in row.ItemArray)
{
if (value != null && value.ToString() != "")
{
return false;
}
}
return true;
}
}
public static void RemoveEmptyRows(this DataTable data)
{
var rowsToDelete = data.Rows.Cast<DataRow>()
.Where(row => row.AllColumnsEmpty())
.ToList();
rowsToDelete.ForEach(row => data.Rows.Remove(row));
}
Usage is then
someDatatable.RemoveEmptyRows();

To remove Empty Rows from DataTable:
dt.Rows.Cast<DataRow>().ToList().FindAll(Row =>
{ return String.IsNullOrEmpty(String.Join("", Row.ItemArray)); }).ForEach(Row =>
{ dt.Rows.Remove(Row); });

Related

No value given for one or more required parameters error - Excel

I am getting data from excel and showing it in DataGridWiew.
I have two textboxes, one is for starting index for first record and other is for last record.
Code works fine. But lets suppose starting record is 1 and ending is 10 when I change 10 to 1 or 2 it gives me an error in this line:
adapter.Fill(dataTable);
Full Code is below:
public DataSet Parse(string fileName)
{
string connectionString = string.Format("provider = Microsoft.Jet.OLEDB.4.0; data source = {0}; Extended Properties = Excel 8.0;", fileName);
DataSet data = new DataSet();
foreach (var sheetName in GetExcelSheetNames(connectionString))
{
using (OleDbConnection con = new OleDbConnection(connectionString))
{
string query = "";
var dataTable = new DataTable();
if(tbStarting.Text.Trim()=="" && tbEnding.Text.Trim() == "")
{
query = string.Format("SELECT * FROM [{0}]", sheetName);
}
else
{
query = string.Format("SELECT * FROM [{0}] where SrNo between " + int.Parse(tbStarting.Text.Trim()) + " and " + int.Parse(tbEnding.Text.Trim()) + " order by SrNo", sheetName);
}
con.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, con);
adapter.Fill(dataTable);
data.Tables.Add(dataTable);
con.Close();
}
}
return data;
}
static string[] GetExcelSheetNames(string connectionString)
{
OleDbConnection con = null;
DataTable dt = null;
con = new OleDbConnection(connectionString);
con.Open();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
String[] excelSheetNames = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheetNames[i] = row["TABLE_NAME"].ToString();
i++;
}
return excelSheetNames;
}
Why this is happening please help me?
Looking at the code, it seems that your procedure is working when you ask to retrieve all the record in each table. But you are not showing which table (Sheet) is actually used afterwars.
Chances are, you are using the first one only.
When you submit some parameters, only one of the tables (Sheets) can fulfill those requirements. The other(s) don't, possibly because a field named [SrNo] is not present.
This causes the More Parameters Required error when trying to apply a filter.
Not related to the error, but worth noting: you don't need to recreate the whole DataSet + DataTables to filter your DataSources.
The DataSet.Tables[N].DefaultView.RowFilter can be used to get the same result without destroying all the objects each time a filter is required.
RowFilter has some limitations in the language (e.g. does not support BETWEEN, Field >= Value1 AND Field <= Value2 must be used), but it's quite effective.
This is a possible setup:
(xDataSet is a placeholder for your actual DataSet)
//Collect the values in the TextBoxes in a string array
private void button1_Click(object sender, EventArgs e)
{
string[] Ranges = new string[] { tbStarting.Text.Trim(), tbEnding.Text.Trim() };
if (xDataSet != null)
FilterDataset(Ranges);
}
private void FilterDataset(string[] Ranges)
{
if (string.IsNullOrEmpty(Ranges[0]) & string.IsNullOrEmpty(Ranges[1]))
xDataSet.Tables[0].DefaultView.RowFilter = null;
else if (string.IsNullOrEmpty(Ranges[0]) | string.IsNullOrEmpty(Ranges[1]))
return;
else if (int.Parse(Ranges[0]) < int.Parse(Ranges[1]))
xDataSet.Tables[0].DefaultView.RowFilter = string.Format("SrNo >= {0} AND SrNo <= {1}", Ranges[0], Ranges[1]);
else
xDataSet.Tables[0].DefaultView.RowFilter = string.Format("SrNo = {0}", Ranges[0]);
this.dataGridView1.Update();
}
I've modified your code you code a bit to handle those requirements.
(I've left here those filters anyway; they're not used, but if you still want them, they are in a working condition)
DataSet xDataSet = new DataSet();
string WorkBookPath = #"[Excel WorkBook Path]";
//Query one Sheet only. More can be added if necessary
string[] WBSheetsNames = new string[] { "Sheet1" };
//Open the Excel document and assign the DataSource to a dataGridView
xDataSet = Parse(WorkBookPath, WBSheetsNames, null);
dataGridView1.DataSource = xDataSet.Tables[0];
dataGridView1.Refresh();
public DataSet Parse(string fileName, string[] WorkSheets, string[] ranges)
{
if (!File.Exists(fileName)) return null;
string connectionString = string.Format("provider = Microsoft.ACE.OLEDB.12.0; " +
"data source = {0}; " +
"Extended Properties = \"Excel 12.0;HDR=YES\"",
fileName);
DataSet data = new DataSet();
string query = string.Empty;
foreach (string sheetName in GetExcelSheetNames(connectionString))
{
foreach (string WorkSheet in WorkSheets)
if (sheetName == (WorkSheet + "$"))
{
using (OleDbConnection con = new OleDbConnection(connectionString))
{
DataTable dataTable = new DataTable();
if ((ranges == null) ||
(string.IsNullOrEmpty(ranges[0]) || string.IsNullOrEmpty(ranges[1])) ||
(int.Parse(ranges[0]) > int.Parse(ranges[1])))
query = string.Format("SELECT * FROM [{0}]", sheetName);
else if ((int.Parse(ranges[0]) == int.Parse(ranges[1])))
query = string.Format("SELECT * FROM [{0}] WHERE SrNo = {1}", sheetName, ranges[0]);
else
query = string.Format("SELECT * FROM [{0}] WHERE (SrNo BETWEEN {1} AND {2}) " +
"ORDER BY SrNo", sheetName, ranges[0], ranges[1]);
con.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, con);
adapter.Fill(dataTable);
data.Tables.Add(dataTable);
};
}
}
return data;
}
static string[] GetExcelSheetNames(string connectionString)
{
string[] excelSheetNames = null;
using (OleDbConnection con = new OleDbConnection(connectionString))
{
con.Open();
using (DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null))
{
if (dt != null)
{
excelSheetNames = new string[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
excelSheetNames[i] = dt.Rows[i]["TABLE_NAME"].ToString();
}
}
}
}
return excelSheetNames;
}

Handling 'NULL' and key constraints when inserting data from a gridview

I'm trying to insert values into the database via gridview from a C# Windows application. I tried 2 different methods but neither seems to be working for me. The 2 type of code is shown below......
Assuming, even if the code below works.... I'm getting various errors regarding the primary key and foreign key constraints.......
Problem:
I have confactorID and macroID columns as integer with nullable in destination businesslogic table....... I'm not sure how to insert 'NULL' in these columns from the C# gridview tool...
Even if I give integer values as input there seems to be foreign key and primary key (duplication) constraint issues....
What do I need to change in my below code to resolve these issues.... I've been stuck with these problem for more than 8 hours... Any help is much appreciated.
Code type 1:
private void ADD_button_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection(sqlconn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
con.Open();
for (int i = 1; i < dataGridView.Rows.Count; i++)
{
string sql = #"INSERT INTO " + schemaName +"ERSBusinessLogic VALUES ("
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_ID"].Value + ", '"
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_Formula"].Value.ToString() + "', "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_InputsCount"].Value + ",'"
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_Inputs"].Value.ToString() + "', "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_ConvFactorID"].Value + ", "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_MacroID"].Value + ", '"
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_DataSeries"].Value.ToString() + "', '"
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_InputTimeDimensionValue"].Value.ToString() + "', "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_InputTimeDimensionType"].Value + ", "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_GeographyDimensionID"].Value + ", "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_InputsUnitsIDs"].Value + ", '"
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_Type"].Value + "', "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_PrivacyID"].Value + ", '"
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_LongDesc"].Value.ToString() + "', '"
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_InputSources"].Value.ToString() + "', '"
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_OutputName"].Value.ToString() + "', "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_OutputUnitID"].Value + ", '"
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_OutputDestination"].Value.ToString() + "', '"
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_OutputTimeDimensionValue"].Value.ToString() + "', "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_OutputTimeDimensionType"].Value + ", "
+ dataGridView.Rows[i].Cells["ERSBusinessLogic_GroupID"].Value + ");";
if ((dataGridView.Rows[i].Cells["ERSBusinessLogic_ConvFactorID"].Value == " ") && (dataGridView.Rows[i].Cells["ERSBusinessLogic_MacroID"].Value == null))
{
Convert.ToInt32(dataGridView.Rows[i].Cells["ERSBusinessLogic_ConvFactorID"].Value = "NULL");
Convert.ToInt32 (dataGridView.Rows[i].Cells["ERSBusinessLogic_MacroID"].Value = "NULL");
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
else
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
finally
{
con.Close();
}
}
Code type 2:
private void ADD_button_Click(object sender, EventArgs e)
{
// Getting data from DataGridView
DataTable myDt = new DataTable();
myDt = GetDTfromDGV(dataGridView);
// Writing to sql
WriteToSQL(myDt);
}
private DataTable GetDTfromDGV(DataGridView dgv)
{
// Making our DataTable
DataTable dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
dt.Columns.Add(column.Name, typeof(string));
}
// Getting data
foreach (DataGridViewRow dgvRow in dgv.Rows)
{
DataRow dr = dt.NewRow();
for (int col = 0; col < dgv.Columns.Count; col++)
{
dr[col] = dgvRow.Cells[col].Value;
}
dt.Rows.Add(dr);
}
// removing empty rows
for (int row = dt.Rows.Count - 1; row >= 0; row--)
{
bool flag = true;
for (int col = 0; col < dt.Columns.Count; col++)
{
if (dt.Rows[row][col] != DBNull.Value)
{
flag = false;
break;
}
}
if (flag == true)
{
dt.Rows.RemoveAt(row);
}
}
return dt;
}
private void WriteToSQL(DataTable dt)
{
using (SqlConnection con = new SqlConnection(sqlconn))
{
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con);
// Setting the database table name
sqlBulkCopy.DestinationTableName = "[AnimalProductsCoSD].[CoSD].[ERSBusinessLogic]";
// Mapping the DataTable columns with that of the database table
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[0].ColumnName, "ERSBusinessLogic_ID"));
Convert.ToString(sqlBulkCopy.ColumnMappings.Add(dt.Columns[1].ColumnName, "ERSBusinessLogic_Formula"));
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[2].ColumnName, "ERSBusinessLogic_InputsCount"));
Convert.ToString (sqlBulkCopy.ColumnMappings.Add(dt.Columns[3].ColumnName, "ERSBusinessLogic_Inputs"));
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[4].ColumnName, "ERSBusinessLogic_ConvFactorID"));
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[5].ColumnName, "ERSBusinessLogic_MacroID"));
Convert.ToString (sqlBulkCopy.ColumnMappings.Add(dt.Columns[6].ColumnName, "ERSBusinessLogic_DataSeries"));
Convert.ToString (sqlBulkCopy.ColumnMappings.Add(dt.Columns[7].ColumnName, "ERSBusinessLogic_InputTimeDimensionValue"));
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[8].ColumnName, "ERSBusinessLogic_InputTimeDimensionType"));
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[9].ColumnName, "ERSBusinessLogic_GeographyDimensionID"));
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[10].ColumnName, "ERSBusinessLogic_InputsUnitsIDs"));
Convert.ToString (sqlBulkCopy.ColumnMappings.Add(dt.Columns[11].ColumnName, "ERSBusinessLogic_Type"));
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[12].ColumnName, "ERSBusinessLogic_PrivacyID"));
Convert.ToString (sqlBulkCopy.ColumnMappings.Add(dt.Columns[13].ColumnName, "ERSBusinessLogic_LongDesc"));
Convert.ToString (sqlBulkCopy.ColumnMappings.Add(dt.Columns[14].ColumnName, "ERSBusinessLogic_InputSources"));
Convert.ToString (sqlBulkCopy.ColumnMappings.Add(dt.Columns[15].ColumnName, "ERSBusinessLogic_OutputName"));
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[16].ColumnName, "ERSBusinessLogic_OutputUnitID"));
Convert.ToString (sqlBulkCopy.ColumnMappings.Add(dt.Columns[17].ColumnName, "ERSBusinessLogic_OutputDestination"));
Convert.ToString (sqlBulkCopy.ColumnMappings.Add(dt.Columns[18].ColumnName, "ERSBusinessLogic_OutputTimeDimensionValue"));
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[19].ColumnName, "ERSBusinessLogic_OutputTimeDimensionType"));
Convert.ToInt32 (sqlBulkCopy.ColumnMappings.Add(dt.Columns[20].ColumnName, "ERSBusinessLogic_GroupID"));
con.Open();
sqlBulkCopy.WriteToServer(dt);
}
}
Thanks
First of all check your database table, columns that keeps IDs from another tables must allow null value like so:
And if your table ID is Identity column with auto increment you don't need to write ID, table automatically add ID.
If everything ok then try to do like so:
private DataTable GetDTfromDGV(DataGridView dgv)
{
// Macking our DataTable
DataTable dt = new DataTable();
//Another way to add columns
dt.Columns.AddRange(new DataColumn[5]
{
//new DataColumn("table_ID", typeof(string)), if table_ID is not Identity column with auto increment then uncomment
new DataColumn("sql_col2", typeof(string)),
new DataColumn("sql_col3", typeof(string)),
new DataColumn("sql_col4", typeof(string)),
new DataColumn("Table_2_ID", typeof(int)),
new DataColumn("Table_3_IDt", typeof(int))
});
// Getting data
foreach (DataGridViewRow dgvRow in dgv.Rows)
{
DataRow dr = dt.NewRow();
for (int col = 1; col < dgv.Columns.Count; col++) //if table_ID is not Identity column with auto increment then start with 0
{
dr[col - 1] = dgvRow.Cells[col].Value == null ? DBNull.Value : dgvRow.Cells[col].Value;
}
dt.Rows.Add(dr);
}
// removing empty rows
....
return dt;
}
private void WriteToSQL(DataTable dt)
{
string connectionStringSQL = "Your connection string";
using (SqlConnection sqlConn = new SqlConnection(connectionStringSQL))
{
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlConn);
// Setting the database table name
sqlBulkCopy.DestinationTableName = "Table_1";
// Mapping the DataTable columns with that of the database table
//sqlBulkCopy.ColumnMappings.Add(dt.Columns[0].ColumnName, "table_ID"); table_ID is Identity column with auto increment
sqlBulkCopy.ColumnMappings.Add(dt.Columns[0].ColumnName, "sql_col2");
sqlBulkCopy.ColumnMappings.Add(dt.Columns[1].ColumnName, "sql_col3");
sqlBulkCopy.ColumnMappings.Add(dt.Columns[2].ColumnName, "sql_col4");
sqlBulkCopy.ColumnMappings.Add(dt.Columns[3].ColumnName, "Table_2_ID");
sqlBulkCopy.ColumnMappings.Add(dt.Columns[4].ColumnName, "Table_3_ID");
sqlConn.Open();
sqlBulkCopy.WriteToServer(dt);
}
}
I tried and that's what I got:
You could use parameterized query.
For example:
var sqlcommand = new SqlCommand
{
CommandText = "INSERT INTO TABLE(Column1,Column2) VALUES(#Column1Value,#Column2Value)"
};
var param1 = new SqlParameter("#Column1Value", SqlDbType.Int)
{
Value = (String.IsNullOrWhiteSpace(dataGridView.Rows[i].Cells["ERSBusinessLogic_ConvFactorID"].Value)) ? DBNull.Value: (object)(dataGridView.Rows[i].Cells["ERSBusinessLogic_ConvFactorID"].Value)
};
var param2 = new SqlParameter("#Column2Value", SqlDbType.Int)
{
Value = (String.IsNullOrWhiteSpace(dataGridView.Rows[i].Cells["ERSBusinessLogic_MacroID"].Value)) ? DBNull.Value : (object)dataGridView.Rows[i].Cells["ERSBusinessLogic_MacroID"].Value)
};
sqlcommand.Parameters.Add(param1);
sqlcommand.Parameters.Add(param2);
If you use method 1 that you tried, you'll probably want to create SqlParameter objects and parameterize your query. Refer to this SO post: Right syntax of SqlParameter. That being said, you just want to get it to work first I'm sure. You could check the value of the DataGridViewCell's Value property for the convFactorID and macroID. If either of these are null, then you can assign a string the text "NULL". For brevity, I've used the C# conditional operator (https://msdn.microsoft.com/en-us/library/ty67wk28.aspx). This is one way you might do what I'm describing:
string convFactorIDText = (dataGridView.Rows[i].Cells["ERSBusinessLogic_ConvFactorID"].Value == null) ? "NULL" : dataGridView.Rows[i].Cells["ERSBusinessLogic_ConvFactorID"].Value.ToString();
string macroIDText = (dataGridView.Rows[i].Cells["ERSBusinessLogic_MacroID"].Value == null) ? "NULL" : dataGridView.Rows[i].Cells["ERSBusinessLogic_MacroID"].Value.ToString();
Then alter you SQL to include the string variables that contain either a actual value or NULL.
string sql = string.Format(#"INSERT INTO {0}.ERSBusinessLogic VALUES ({1}, '{2}', {3}, {4}, {5}, {6}"),
schemaName,
dataGridView.Rows[i].Cells["ERSBusinessLogic_ID"].Value,
dataGridView.Rows[i].Cells["ERSBusinessLogic_Formula"].Value.ToString(),
dataGridView.Rows[i].Cells["ERSBusinessLogic_InputsCount"].Value,
dataGridView.Rows[i].Cells["ERSBusinessLogic_Inputs"].Value,
convFactorIDText,
macroIDText
// and so forth
);

how to delete first field and all values of this field in .CSV file using C#

I want to load .csv file and then import to SQl database. But before I do that, I want to do something to delete first field (in header) and also all values of this field.
this below is the example of my .csv file :
> TableId|PERIODE|DATEBALANCEASOF|ACCCODE|CUSTNAME|CUSTGROUP|
> TB_001|201501|2015-01-01|11-0001|DYNAMIC EXPRESS|11|
> TB_001|201501|2015-01-01|11-0002|DYNAMIC EXPRESS|12|
> TB_001|201501|2015-01-01|11-0003|DYNAMIC EXPRESS|13|
> TB_001|201501|2015-01-01|11-0004|DYNAMIC EXPRESS|14|
before I import that .csv file, I need my .csv file willbe like this below :
PERIODE|DATEBALANCEASOF|ACCCODE|CUSTNAME|CUSTGROUP|
201501|2015-01-01|11-0001|DYNAMIC EXPRESS|11|
201501|2015-01-01|11-0002|DYNAMIC EXPRESS|12|
201501|2015-01-01|11-0003|DYNAMIC EXPRESS|13|
201501|2015-01-01|11-0004|DYNAMIC EXPRESS|14|
this below is my code :
public void readCSVManual(string pathLocalSuccess, string pathHistory, string modul)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = #"server=" + serverDB + "; database=" + DB + "; Trusted_Connection=" + trustedConnection + "; user=" + UserDB + "; password=" + PassDB + "";
string[] files = Directory.GetFiles(pathLocalSuccess);
if (files == null)
{
MessageBox.Show("Files not found");
}
foreach (string file in files)
{
FileInfo fileInf = new FileInfo(file);
string filename = fileInf.Name;
StreamReader reader = new StreamReader(file);
string line = reader.ReadLine();
string[] value = line.Split('|');
var list = new List<string>(value);
list.RemoveAt(0);
value = list.ToArray();
//string[] v = string(nValue.ToArray());
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
while (!reader.EndOfStream)
{
value = reader.ReadLine().Split('|');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "ACC_004";
bc.BatchSize = dt.Rows.Count;
bc.WriteToServer(dt);
bc.Close();
con.Close();
reader.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Please help me to resolve this problem..
Thanks for everyone who respons my question,
Actually I already solve this problem using a little editing in my code
this below is my code and works for me :
public void readCSVAutomatic(string pathLocalSuccess, string pathHistory, string pathLogFolderSuccess, string pathErrorLog, string modul)
{
try
{
string[] files = Directory.GetFiles(pathLocalSuccess);
foreach (string file in files)
{
FileInfo fileInf = new FileInfo(file);
string filename = fileInf.Name;
StreamReader reader = new StreamReader(file);
string line = reader.ReadLine();
string[] value = line.Split('|');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
while (!reader.EndOfStream)
{
//value = value.
value = reader.ReadLine().Split('|');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
string xTableName = dt.Rows[0].ItemArray[0].ToString();
string xPeriode = dt.Rows[0].ItemArray[1].ToString();
dt.Columns.RemoveAt(0);
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=" + serverDB + "; Initial Catalog=" + DB + "; Trusted_Connection=" + trustedConnection + "; user=" + UserDB + "; password=" + PassDB + "";
con.Open();
SqlCommand com = con.CreateCommand();
string strDelete = "DELETE FROM " + xTableName + " WHERE PERIODE='" + xPeriode + "'";
com.CommandText = strDelete;
com.Connection = con;
com.ExecuteNonQuery();
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = xTableName;
bc.BatchSize = dt.Rows.Count;
bc.WriteToServer(dt);
bc.Close();
con.Close();
reader.Close();
moveFileAfterImported(pathLocalSuccess, filename, pathHistory);
createLogCSVSuccessImported(pathLogFolderSuccess, "File Imported","Message");
}
}
catch(Exception ex)
{
createErrorLogImportCSV(pathErrorLog, "ErrorImportCSV", ex.ToString());
}
}
This seems to do the job for me:
First start by reading in all the files:
var datatables =
Directory
.GetFiles(pathLocalSuccess)
.Select(file => File.ReadAllLines(file).Select(x => x.Split('|')).ToArray())
.Select(lines => new
{
headers = lines[0].Skip(1).ToArray(),
lines = lines.Skip(1).Select(l => l.Skip(1).ToArray()).ToArray(),
})
.Select(x =>
{
var dt = new DataTable();
foreach (var dc in x.headers)
{
dt.Columns.Add(new DataColumn(dc));
}
foreach (var line in x.lines.Skip(1).Where(y => y.Length == x.headers.Length))
{
var row = dt.NewRow();
row.ItemArray = line;
dt.Rows.Add(row);
}
return dt;
})
.ToArray();
Then it's a simple matter of loading all the data. This code only opens the connection once. It should be quite fast.
if (!datatables.Any())
{
MessageBox.Show("Files not found");
}
else
{
using (var con = new SqlConnection())
{
con.ConnectionString = #"server=" + serverDB + "; database=" + DB + "; Trusted_Connection=" + trustedConnection + "; user=" + UserDB + "; password=" + PassDB + "";
foreach (var dt in datatables)
{
var bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "ACC_004";
bc.BatchSize = dt.Rows.Count;
bc.WriteToServer(dt);
bc.Close();
}
con.Close();
}
}
The important part for skipping the first field is found in this code:
.Select(lines => new
{
headers = lines[0].Skip(1).ToArray(),
lines = lines.Skip(1).Select(l => l.Skip(1).ToArray()).ToArray(),
})

How to ignore the empty cell in the GridView

I have a GridView and Edit link for each row. I can click on Edit and fill data in the cells of GridView and can Update the GridView row and the corresponding table in the database is also updated.
I have a save button, which on_click reads each and every row, column by column and perform some action.
The function works fine if all the cell in the GridView has some data filled in it. If the cell in the GridView is empty, it gives and error : Input String is not in the correct format.
The code I have tried is :
protected void SAVE_GRID_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView2.Rows)
{
string Loc = row.Cells[1].Text;
string strg = "SELECT Location_Type FROM Quantity WHERE Locations='" + Loc + "'";
SqlCommand com = new SqlCommand(strg, con);
con.Open();
SqlDataReader sdr = com.ExecuteReader();
while (sdr.Read())
{
Loctype.Text = sdr[0].ToString().Trim();
}
con.Close();
for (int i = 1; i < GridView2.Columns.Count; i++)
{
String header = GridView2.Columns[i].HeaderText;
string str = "SELECT Profile_Type FROM Profile_Tooltip WHERE Profile_Name='"+header+"'";
SqlCommand cmd = new SqlCommand(str,con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
profiletype.Text = dr[0].ToString().Trim();
}
con.Close();
if (!string.IsNullOrEmpty(row.Cells[i + 1].Text.Trim()))
{
int n = Convert.ToInt16(row.Cells[i + 1].Text);
//int n = int.Parse(row.Cells[i].Text);
for (int m = Asset_List.Items.Count - 1; m >= 0; m--)
{
Asset_List.Items.Remove(Asset_List.Items[m]);
}
for (int j = 1; j <= n; j++)
{
Asset_List.Items.Add(string.Concat(profiletype.Text, j));
}
for (int k = 0; k <= Asset_List.Items.Count - 1; k++)
{
com = new SqlCommand("INSERT INTO " + Label3.Text + " VALUES ('" + Loctype.Text + "','" + Loc + "','" + header + "','" + Asset_List.Items[k] + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
}
}
SqlCommand comd = new SqlCommand("SELECT * FROM " + Label3.Text + "", con);
SqlDataAdapter da = new SqlDataAdapter(comd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
I want to check if the cell is empty, if empty I want to increment to next cell without performing any action for the empty cell.
Kindly help me solve this problem. Thank you.
just use trim and sting function
if (!string.IsNullOrEmpty(row.Cells[i].Text.Trim() ) )
you need to check here
int n = Convert.ToInt16(row.Cells[i + 1].Text);
check the string is convertible to integer or not using parse or tryparse methods of framework
short n = 0;
if(Int16.TryParse(row.Cells[i + 1].Text,out n);)
{
//perform function and user n here now
}
The line that sticks out the most to me is this one:
int n = Convert.ToInt16(row.Cells[i + 1].Text);
I usually use something like this to avoid exceptions:
int n = 0;
bool didParse = Int16.TryParse(row.Cells[i + 1].Text, out n);
if (didParse)
{
//add code here
}
Your error might be an exception being thrown because your cell text is empty or perhaps even null.

Microsoft OLEDB Error External table is not in the expected format

I have wrote a code that can read an excel 2007 file using Microsoft Data Access Engine the below code snippet works fine for most of the files but it happens to work for most of the excel files i.e. .xlsx,.xls but when it fails at objConn.Open(); for excel files that have excel formatting problems please refer to the image below
It would failed to open the ole Db Connection stating error External table is not in the expected format . One more problem with this import procedure is that
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM "+ SelectionSheet, objConn);
is not able to read sheets starting with spaces any help with solving this issue would be highly appreciated.
public DataTable ReadExcel(string Path, ArrayList IgnoreString, ArrayList IgnoreColumn)
{
DataTable dtReturn = new DataTable();
DataTable dtPrintable = new DataTable();
DataTable dtTemp = new DataTable();
try
{
string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + Path + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
DataTable dtSheetnames = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
DataTable dtTesting = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.DbInfoLiterals, new object[] {});
DataTable dtTesting2 = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, new object[] { });
string SelectionSheet = dtSheetnames.Rows[0][2].ToString();
if (SelectionSheet.Contains("'") )
{
SelectionSheet = SelectionSheet.Remove(0, 1);
SelectionSheet = "[" + SelectionSheet;
SelectionSheet = SelectionSheet.Remove(SelectionSheet.Length - 1, 1);
// -- Mod by zeemz on 23 dec
// string PrintArea = SelectionSheet + "Print_Area]";
SelectionSheet = SelectionSheet + "]";
}
else
{
SelectionSheet = "["+ SelectionSheet + "]";
}
//OleDbCommandBuilder objCmdBuilder = new OleDbCommandBuilder(
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM "+ SelectionSheet, objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
DataSet objDataSet = new DataSet();
DataSet PrintAreads = new DataSet();
objAdapter.SelectCommand = objCmdSelect;
objAdapter.Fill(objDataSet);
// -- Mod by zeemz on 23 dec
//objCmdSelect.CommandText = "SELECT * FROM " + PrintArea;
//objAdapter.Fill(PrintAreads);
objConn.Close();
dtReturn = objDataSet.Tables[0].Copy();
// dtPrintable = PrintAreads.Tables[0].Copy();
// -- Mod by zeemz on 23 dec
//if (dtPrintable.Columns.Count != dtReturn.Columns.Count)
//{
// int TotalPrintable = dtPrintable.Columns.Count;
// int TotalComing = dtReturn.Columns.Count;
// int StartRemovingPos = TotalComing - TotalPrintable;
// for (int i = TotalPrintable; dtPrintable.Columns.Count != dtReturn.Columns.Count; i++)
// {
// dtReturn.Columns.RemoveAt(i);
// i = i - 1 ;
// }
//}
int iCount = 0;
while (iCount <= dtReturn.Rows.Count - 1)
{
if (isRowEmpty(dtReturn.Rows[iCount]))
{
dtReturn.Rows.RemoveAt(iCount);
}
else
{
iCount += 1;
}
}
//now applying the filters
//column ignore
for (int i = IgnoreColumn.Count - 1; i >= 0; i--)
{
dtReturn.Columns.RemoveAt((int)IgnoreColumn[i]);
}
//string ignore
for (int i = IgnoreString.Count - 1; i >= 0; i--)
{
for (int j = dtReturn.Rows.Count - 1; j >= 0; i--)
{
foreach (DataColumn dCol in dtReturn.Columns)
{
if (dtReturn.Rows[j][dCol.ColumnName].ToString().ToLower().Contains(IgnoreString[i].ToString().ToLower()))
{
dtReturn.Rows.RemoveAt(j);
break;
}
}
}
}
/* Hack to get rid of DateTime Columns */
// added by zeemz
dtTemp = dtReturn.Clone();
dtTemp.Clear();
foreach (DataColumn tempColumn in dtTemp.Columns)
{
// if (tempColumn.DataType == typeof(DateTime))
// {
tempColumn.DataType = typeof(String);
// }
}
foreach (DataRow tempRow in dtReturn.Rows)
{
DataRow insRow = dtTemp.NewRow();
foreach (DataColumn tempColumn in dtReturn.Columns)
{
if (tempColumn.DataType == typeof(DateTime))
{
if (!String.IsNullOrEmpty(tempRow[tempColumn.ColumnName.ToString()].ToString()))
{
insRow[tempColumn.ColumnName.ToString()] = Convert.ToDateTime(tempRow[tempColumn.ColumnName.ToString()].ToString()).ToString("yyyyMMddhhmmss");
}
else
{
insRow[tempColumn.ColumnName.ToString()] = "";
}
}
else
{
insRow[tempColumn.ColumnName.ToString()] = tempRow[tempColumn.ColumnName.ToString()].ToString();
}
}
dtTemp.Rows.Add(insRow);
}
}
catch (Exception ex)
{
throw ex;
}
return dtTemp;
}
I happen to find out the problem exists when you modify the xlsx file manually or programatically without using excel once you modify the xlsx the format doesn't stay intact and the above mentioned error comes up because OleDbDataAdapter is unable to handle the modified file and Excel it self can fix the corrupt files shows the file as expected.

Categories

Resources