Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

if excel sheet having empty column, then how can i insert null value in database for the empty column in excel.
Posted
Comments
Honey Jagyasi 23-Apr-14 6:35am    
In query u can handle null with blank. IsNull("ColumnName",'')

while creating columns in table , you add coulumnName Datatype = NULL

for ex:

empname varchar(200)=NULL
 
Share this answer
 
Comments
Member 10454499 23-Apr-14 6:21am    
am allowing null values in db table, even though it is showing error sir
Honey Jagyasi 23-Apr-14 6:23am    
if the column value expects int then it would give u error. for varchar it will accept null.
Member 10454499 23-Apr-14 6:39am    
all string columns only their in db table sir
nandakishoreroyal 23-Apr-14 6:36am    
Dear Honey Jagyasi,

i just gave an example varchar , in same way for integer also,

Ex: empid int=NULL
Please post the code what you have tried..
 
Share this answer
 
Comments
Member 10454499 23-Apr-14 6:45am    
Utility ut = new Utility();

string xls = ut.UploadData(Constants.UploadData, fleData);

string absPath = Server.MapPath("~/Uploads/" + xls);
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;";
connectionString += "Data Source=";
connectionString += absPath + ";";
connectionString += @"Extended Properties=""Excel 12.0;HDR=YES;IMEX=1""";

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetname = dt.Rows[0]["Table_Name"].ToString();
string query = "SELECT * FROM [" + sheetname + "]";
OleDbCommand ocmd = new OleDbCommand(query, conn);
OleDbDataReader rdr = ocmd.ExecuteReader();

//*******************************************************
string conString = Constants.CONNECTION;
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
while (rdr.Read())
{
string name = Convert.ToString(rdr[1]);
string organization = Convert.ToString(rdr[2]);
string intrestedIn = Convert.ToString(rdr[3]);
string phone = Convert.ToString(rdr[4]);
string email = Convert.ToString(rdr[5]);
string address = Convert.ToString(rdr[6]);
string city = Convert.ToString(rdr[7]);
string comments = Convert.ToString(rdr[8]);
DateTime dateofEntry = Convert.ToDateTime(rdr[9]);

using (SqlCommand cmd = new SqlCommand("insert into tblTempData values(@Name,@Organization,@IntrestedIn,@Phone,@Email,@Address,@City,@Comments,@DateOfEntry)", con))
{
cmd.Parameters.AddWithValue("@Name", SqlDbType.VarChar).Value = name;
cmd.Parameters.AddWithValue("@Organization", SqlDbType.VarChar).Value = organization;
cmd.Parameters.AddWithValue("@IntrestedIn", SqlDbType.VarChar).Value = intrestedIn;
cmd.Parameters.AddWithValue("@Phone", SqlDbType.VarChar).Value = phone;
cmd.Parameters.AddWithValue("@Email", SqlDbType.VarChar).Value = email;
cmd.Parameters.AddWithValue("@Address", SqlDbType.VarChar).Value = address;
cmd.Parameters.AddWithValue("@City", SqlDbType.VarChar).Value = city;
cmd.Parameters.AddWithValue("@Comments", SqlDbType.VarChar).Value = comments;
cmd.Parameters.AddWithValue("@DateOfEntry", SqlDbType.DateTime).Value = dateofEntry;

cmd.ExecuteNonQuery();
}
}

}
}
Honey Jagyasi 23-Apr-14 6:58am    
cmd.Parameters.Add(new SqlParameter("@Name", DBNull.Value));
Member 10454499 23-Apr-14 7:08am    
No sir, it is not working
CHill60 23-Apr-14 8:00am    
Please use the Have Question or Comment links rather than posting remarks as "solutions"
try like that,

C#
#region"BULK UPLOAD DATA TO DB TABLE                  "
      private static void BulkUploadDataToDBTable(DataTable dtData, int FileID)
      {
          string strSource = "DumpDataFromSourceFiles";
          try
          {
              if (dtData != null && dtData.Rows.Count > 0)
              {
                  myLogFile.LogMessage((int)AppLog.LogLevel.DEBUG, strSource, "Started bulkuploading the records in datatable to PhoneRSVP table");
                  string sqlConnectionString = Constants.DBConn;
                  SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
                  sqlConnection.Open();

                  //Adding the PhoneRSVPFileID column to the datatable
                  DataColumn dcFileID = new DataColumn();
                  dcFileID.DataType = typeof(int);
                  dcFileID.DefaultValue = FileID;
                  dcFileID.ColumnName = "PhoneRSVPFileID";
                  dtData.Columns.Add(dcFileID);

                  //Adding the CreatedOn column to the datatable
                  DataColumn dcCreatedOn = new DataColumn();
                  dcCreatedOn.DataType = typeof(DateTime);
                  dcCreatedOn.DefaultValue = DateTime.Now;
                  dcCreatedOn.ColumnName = "CreatedOn";
                  dtData.Columns.Add(dcCreatedOn);


                  //to clear existing data from table
                  SqlCommand cmd = new SqlCommand("Delete from PhoneRSVP", sqlConnection);
                  cmd.ExecuteNonQuery();

                  using (SqlBulkCopy bcp = new SqlBulkCopy(sqlConnectionString, SqlBulkCopyOptions.FireTriggers))
                  {
                      bcp.ColumnMappings.Add("CUST_SKEY", "CUST_SKEY");
                      bcp.ColumnMappings.Add("APPNUM_SKEY", "APPNUM_SKEY");
                      bcp.ColumnMappings.Add("INVITEE_FIRST_NAME", "INVITEE_FIRST_NAME");
                      bcp.ColumnMappings.Add("INVITEE_LAST_NAME", "INVITEE_LAST_NAME");
                      bcp.ColumnMappings.Add("STORE_NUMBER", "STORE_NUMBER");
                      bcp.ColumnMappings.Add("EVENT_DATE", "EVENT_DATE");
                      bcp.ColumnMappings.Add("EVENT_TIME", "EVENT_TIME");
                      bcp.ColumnMappings.Add("GUESTS", "GUESTS");
                      bcp.ColumnMappings.Add("RSVP_DATETIME_RESPONSE", "RSVP_DATETIME_RESPONSE");
                      bcp.ColumnMappings.Add("VRU_CSR", "VRU_CSR");
                      bcp.ColumnMappings.Add("EVENT_CODE", "EVENT_CODE");
                      bcp.ColumnMappings.Add("PhoneRSVPFileID", "PhoneRSVPFileID");
                      bcp.ColumnMappings.Add("CreatedOn", "CreatedOn");


                      bcp.DestinationTableName = "PhoneRSVP";

                      bcp.BulkCopyTimeout = 0;
                      bcp.BatchSize = 1000;

                      bcp.WriteToServer(dtData);
                  }
                  myLogFile.LogMessage((int)AppLog.LogLevel.DEBUG, strSource, "Records in datatable bulkuploaded into PhoneRSVP table successfully.");
                  sqlConnection.Close();
              }
          }
          catch (Exception ex)
          {
              //MoveToArchive(filePath, false);
              myLogFile.LogMessage((int)AppLog.LogLevel.ERROR, strSource, ex.Message);
              errfunName = strSource;
              throw ex;
          }
      }
      #endregion
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900