0

I want to write a program that save all the data from a .csv into a database.

I use a service-based database.

But when I want to use the addtoTablesName like what I used to do and I learned at university, it suddenly throws an error.

This is my code:

class Program
{
    static void Main(string[] args)
    {
        Database1Entities1 dbb = new Database1Entities1();//object database
        excel.Application xlApp;
        excel.Workbook xlWb;
        excel._Worksheet xlWs;
        excel.Range xlRange;

        excel.Application xlApp1;
        excel.Workbook xlWb1;
        excel._Worksheet xlWs1;
        excel.Range xlRange1;

        int rowFile = 0;
        int colFile = 0;
        string[][] dataFile;
        DateTime date = DateTime.Now;
        string tgl = date.Date.ToString("dd");
        string bln = date.Month.ToString("d2");
        string thn = date.Year.ToString();
        string tglskrg = thn+bln+tgl;
        string[] items;
        DateTime otime2 = DateTime.ParseExact(tglskrg, "yyyyMMdd", null);

        xlApp = new excel.Application();

        string[] filesindirectory = Directory.GetDirectories(@"C:\Users\u532246\Desktop\TRAVELOKA");

        foreach (string files in filesindirectory)
        {
            string name = Path.GetFileName(files);
            string subname = name.Substring(10, 8);
            DateTime subTime = DateTime.ParseExact(subname, "yyyyMMdd", null);
            TimeSpan span = otime2.Subtract(subTime);

            if(span.Days < 2)
            {
                // Console.WriteLine(files);
                foreach(string subfiles in Directory.GetFiles(files))
                {
                    xlWb = xlApp.Workbooks.Open(subfiles);
                    xlWs = xlWb.Sheets[1];
                    xlRange = xlWs.UsedRange;
                    rowFile = xlRange.Rows.Count;
                    colFile = xlRange.Columns.Count;

                    dataFile = new string[rowFile][];

                    for(int i = 0; i < dataFile.Length; i++)
                    {
                        dataFile[i] = new string[colFile];
                    }

                    for(int i = 0; i<rowFile;i++)
                    {
                        for(int j = 0; i<colFile;j++)
                        {
                            try
                            {
                                dataFile[i][j] = xlWs.Cells[i + 1, j + 1].value2.ToString();
                                //MessageBox.Show(dataMapping[i][j]);
                            }
                            catch (Exception ee)
                            {
                                dataFile[i][j] = "";
                            }
                        }
                    }
                    CMRTable cmr = new CMRTable();
                    for(int i = 0; i < rowFile; i++)
                    {
                        items = dataFile[i][0].Split(';');
                        cmr.Id = i + 1;
                        cmr.Merchant_Name = items[1];
                        cmr.Sett_Date = items[2];
                        cmr.Proc_Date = items[3];
                        cmr.Mid = items[4];
                        cmr.CardType = items[5];
                        cmr.Trx_Date = items[6];
                        cmr.Jam_Trx = items[7];
                        cmr.Auth = items[8];
                        cmr.CardNo = items[9];
                        cmr.Trx_Type = items[10];
                        cmr.Amount = items[11];
                        cmr.Rate = items[12];
                        cmr.Disc_Amt = items[13];
                        cmr.Tenor_Ins = items[14];
                        cmr.Rate_Ins = items[15];
                        cmr.Disc_Ins = items[16];
                        cmr.Net_Amt = items[17];
                        cmr.Purchase_Id = items[18];
                        cmr.Mechant_Descriptor = items[19];
                        dbb.AddToCMRTables(cmr); #I CANT USE THIS FUNCTION , DONT KNOW WHY 
                        dbb.SaveChanges();
                    }

                    Console.WriteLine(subfiles);
                } 
            }
        }
    }
}

Can someone help me? I think I already do everything exactly like I did in university ..

EDIT: here's the error I get:

Database1Entities1 does not contain a definition for AddToCMRTables and no accessible extension method AddToCMRTables accepting a first argument of type Database1Entities1 could be found

I copy what i used to do in university (here's the code)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{//dipakai ketika ingin menginsert data ke database.
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            displayData();

        }
        Database1Entities db = new Database1Entities();//object database
        private void displayData() 
        {
            var query = from x 
                           in db.MsStudents 
                        select x;
            dataGridView1.DataSource = query;
        }



        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtID.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
            txtName.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
        }//buat ngeklik yg ada di data grid view

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            search(txtSearch.Text);
        }
        public void search(string kata) {
            var query = from x
                        in db.MsStudents
                        where x.Name.Contains(kata)//apa yang muncul di kotak search itu di select
                        select x;
            dataGridView1.DataSource = query;
        }//function buat search

        private void btnInsert_Click(object sender, EventArgs e)
        {
            MsStudent student = new MsStudent();

            student.ID = Int32.Parse(txtID.Text);
            student.Name = txtName.Text;
            db.AddToMsStudents(student);
            db.SaveChanges();

            displayData();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            foreach (Control a in this.Controls)
            {
                if (a.GetType() == typeof(TextBox))
                {

                    a.Text = "";
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
11
  • What error are you getting ? Commented Dec 23, 2019 at 9:25
  • @Cid database1entities1 does not contain a definition for addtocmrtables and no accessible extension method addtocmrtables accepting a first argument of type database1entities1 could be found Commented Dec 23, 2019 at 9:29
  • Please edit your question for additional information instead of posting it as comment. Commented Dec 23, 2019 at 9:30
  • @KlausGütter thanks for the advice, already update it Commented Dec 23, 2019 at 9:31
  • 1
    @trytocode What does AddToCMRTables method look like? It seems that you are missing a reference to this method. Take a look at this question: stackoverflow.com/questions/8835434/… Commented Dec 23, 2019 at 9:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.