Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8766895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:39:47+00:00 2026-06-13T16:39:47+00:00

I am importing an Excel sheet into a SQL Server database. I am able

  • 0

I am importing an Excel sheet into a SQL Server database. I am able to upload the data but there is a column in Excel which consists of dates in format mm/dd/yyyy. I want this to be converted to yyyy/mm/dd before sending it to database :

DataTable dt7 = new DataTable();
dt7.Load(dr);
DataRow[] ExcelRows = new DataRow[dt7.Rows.Count];

// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
  bulkCopy.DestinationTableName = "ExcelTable";
  dt7.Rows.CopyTo(ExcelRows, 0);

  for (int i = 0; i < ExcelRows.Length; i++)
  {
    DateTime oldDate =Convert.ToDateTime(ExcelRows[i]["data"]).Date;
    DateTime newDate = Convert.ToDateTime(oldDate).Date;
    ExcelRows[i]["data"] = newDate.ToString("yyyy/MM/dd");
  }
  bulkCopy.WriteToServer(ExcelRows);

Error at: DateTime oldDate = Convert.ToDateTime(ExcelRows[i][“data”]).Date;
Error: Object cannot be cast from DBNull to other types.

It seems to me that it is reading wrong column or something since it says cannot be cast from DBnull. The Excel sheet has two columns:

id | data (which is date)

I have tried replacing ExcelRows[i]["data"] with ExcelRows[i][1] but I got the same error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:39:48+00:00Added an answer on June 13, 2026 at 4:39 pm

    This happens because ExcelRows[i][“data”] returns DBNull.

    You should handle it when that happens

        if (ExcelRows[i]["data"] == DBNull.Value)
        {
            // Include any actions to perform if there is no date
        }
        else
        {
            DateTime oldDate = Convert.ToDateTime(ExcelRows[i]["data"]).Date;
            DateTime newDate = Convert.ToDateTime(oldDate).Date;
            ExcelRows[i]["data"] = newDate.ToString("yyyy/MM/dd");
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am importing an excel sheet into my sql database. Everything works fine, but
I am importing excel data into sql server using SqlbulkCopy, but the problem is
I am importing excel sheet into sql server database the excelsheet contains 3 columns
I have some questions about importing data from Excel/CSV File into SQL Server. Let
I am importing excelsheet into sqlserver database but there are three columns in excel:
i am preparing myself for importing data from excel sheet to sql server 2005.I
i am currently working on importing data from excel sheet to sql server.i have
I'm importing a .txt file into a Microsoft Excel sheet, it works great, but
I am working on importing data from an Excel sheet to database. The Excel
I'm importing the contents of an Excel 2007 file into a MySQL database but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.