I have a simple button event to import excel sheet into the database. The piece/part of code is like this..
private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Text File";
theDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.xltm;*.xltx";
theDialog.InitialDirectory = @"C:\";
if (theDialog.ShowDialog() == DialogResult.OK)
{
try
{
foreach (var worksheetx in Workbook.Worksheets(theDialog.FileName.ToString()))
{
foreach (var row in worksheetx.Rows)
{
String Temp = @"INSERT INTO [dbo].[myTable]
([Status]
,[NName]
,[Notes]
)
VALUES
('<Status>'
,'<NName>'
,'<Notes>'
)";
String Temp1 = Temp;
bool ForceContinue = false;
foreach (var cell in row.Cells)
{
if (cell != null)
{
if (cell.ColumnIndex == 0)Temp1 = Temp1.Replace("<Status>", cell.Text);
if (cell.ColumnIndex == 1)Temp1 = Temp1.Replace("<NName>", cell.Text);
if (cell.ColumnIndex == 2)Temp1 = Temp1.Replace("<Notes>", cell.Text);
}
else
{
//Looking for this part- How to insert 'NULL' or Blank cell.Text
}
}
DBConn.Execute(Temp1);
For example if my excel sheet column - 'Notes' is like
| Check |
| |
| |
It is currently inserted in DB like
| Check |
|<Notes>|
|<Notes>|
I want it to be like this where nulls are inserted as blanks
| Check |
| |
| |