the name function does not exists in the current context c# error

c# sqlbulkcopy

Question

I wrote a program to SqlBulkCopy To Import CSV Data Into MYSQL database. In the function InsertDataIntoSQLServerUsingSQLBulkCopy triggering some errors as below snapshot.enter image description here

I attached my source code down below. The errors are triggering on line 54.

 using System;
using System.Data;
using Microsoft.VisualBasic.FileIO;
using System.Data.SqlClient;

namespace ReadDataFromCSVFile
{
    static class Program
    {
        static void Main()
        {
            string csv_file_path = @"C:\Users\source\repos\WindowsService1\WindowsService1\bin\Debug\data.csv";
            DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);
            Console.WriteLine("Rows count:" + csvData.Rows.Count);
            Console.ReadLine();
        }
        private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
        {
            DataTable csvData = new DataTable();
            try
            {
                using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
                {
                    csvReader.SetDelimiters(new string[] { "," });
                    csvReader.HasFieldsEnclosedInQuotes = true;
                    string[] colFields = csvReader.ReadFields();
                    foreach (string column in colFields)
                    {
                        DataColumn datecolumn = new DataColumn(column);
                        datecolumn.AllowDBNull = true;
                        csvData.Columns.Add(datecolumn);
                    }
                    while (!csvReader.EndOfData)
                    {
                        string[] fieldData = csvReader.ReadFields();
                        //Making empty value as null
                        for (int i = 0; i < fieldData.Length; i++)
                        {
                            if (fieldData[i] == "")
                            {
                                fieldData[i] = null;
                            }
                        }
                        csvData.Rows.Add(fieldData);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return csvData;
        }

       **line 54  function static void InsertDataIntoSQLServerUsingSQLBulkCopy(DataTable csvFileData)             {
            using (SqlConnection dbConnection = new SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=MorganDB; Integrated Security=SSPI;"))
            {
                dbConnection.Open();
                using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
                {
                    s.DestinationTableName = "table1";

                    foreach (var column in csvFileData.Columns)
                        s.ColumnMappings.Add(column.ToString(), column.ToString());

                    s.WriteToServer(csvFileData);
                }
            }

        }
    }
}

If anyone could catch the error I'd be really appreciated. Thank you!

Accepted Answer

Are you getting confused with Visual BASIC? "function" is not a C# keyword. Replace "function" with either "private" or "public", depending on the use case.

Also, you should be able to use "System.IO" instead of "Microsoft.VisualBasic.FileIo" in the includes.


Popular Answer

I presume you've added the '**Line 54' to indicate where the error is?

From your screen grab it looks like the text 'function' is present in the below though.

   **line 54  function static void InsertDataIntoSQLServerUsingSQLBulkCopy(DataTable csvFileData)             {
        using (SqlConnection dbConnection = new SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=MorganDB; Integrated Security=SSPI;"))
        { 

In C# you don't need to (in fact can't) declare 'function' like you do in VB. The line should be:

static void InsertDataIntoSQLServerUsingSQLBulkCopy(DataTable csvFileData)             
{
   // Rest of code
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why