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 9034479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:29:17+00:00 2026-06-16T08:29:17+00:00

I have the following code for reading from a file and populating my database.

  • 0

I have the following code for reading from a file and populating my database. Everything works fine except that all records are inserted twice.

OleDbCommand c = new OleDbCommand();
c.Connection = GetConnection();
c.CommandText =
    @"CREATE TABLE patients (
        patientid AUTOINCREMENT PRIMARY KEY,
        firstlastname CHAR(50),
        birthdate CHAR(50),
        birthplace CHAR(50),
        gender CHAR(1),
        bloodtype CHAR(50),
        telnum CHAR(50),
        address CHAR(255)
    )";
c.ExecuteNonQuery();

string info = "";
string name = "";
string date = "";
string place = "";
string blood = "";
string num = "";
string address = "";
//Read from file and insert values to patient table
StreamReader myReader = new StreamReader("myPath/patient.txt");
while (true)
{
    info = myReader.ReadLine();
    if (info == null) break;

    if (info.Trim() != String.Empty)
    {
        string[] words = info.Split(',');
        if (words.Length == 6)
        {
            name = words[0].Trim();
            date = words[1].Trim();
            place = words[2];
            blood = words[3];
            num = words[4];
            address = words[5];
        }
    }

    string cmdText = "INSERT INTO patients (" +
      "firstlastname, birthdate, birthplace, bloodtype, telnum, address) " +
      "VALUES (?,?,?,?,?,?)";

    using (OleDbConnection cn = GetConnection())
    {

        using (OleDbCommand cmd = new OleDbCommand(cmdText, cn))
        {
            cmd.Parameters.AddWithValue("name", name);
            cmd.Parameters.AddWithValue("date", date);
            cmd.Parameters.AddWithValue("place", place);
            cmd.Parameters.AddWithValue("blood", blood);
            cmd.Parameters.AddWithValue("num", num);
            cmd.Parameters.AddWithValue("address", address);
            cmd.ExecuteNonQuery();
        }
    }


}

I noticed that the line
if (info.Trim() != String.Empty)

returns “false” every other time so records are inserted twice but dont know how to fix this.

How can i eliminate these duplicates?.
Thanks

EDIT: File contents are as such showing name, date, city, blood type, mobile num and address:

Mehtap Selim, 11/06/1985, Kyrenia, FBRh+, 05617584509, Yasemin St. No:48 Edremit Kyrenia KKTC

  • 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-16T08:29:19+00:00Added an answer on June 16, 2026 at 8:29 am

    What it looks like is that your code structure is still allowing a write to your database even if the conditional statement is false and since the data is still in your variables from the previous write it writes them again. Try enclosing the Database writing into the same conditional. As for why it is coming up false every other time we would need to see the contents of your file.

    i.e.

    if (info.Trim() != String.Empty)
    {
        string[] words = info.Split(',');
        if (words.Length == 6)
        {
            name = words[0].Trim();
            date = words[1].Trim();
            place = words[2];
            blood = words[3];
            num = words[4];
            address = words[5];
        }
    
    
        string cmdText = "INSERT INTO patients (" +
           "firstlastname, birthdate, birthplace, bloodtype, telnum, address) " +
           "VALUES (?,?,?,?,?,?)";
    
        using (OleDbConnection cn = GetConnection())
        {
            using (OleDbCommand cmd = new OleDbCommand(cmdText, cn))
            {
                cmd.Parameters.AddWithValue("name", name);
                cmd.Parameters.AddWithValue("date", date);
                cmd.Parameters.AddWithValue("place", place);
                cmd.Parameters.AddWithValue("blood", blood);
                cmd.Parameters.AddWithValue("num", num);
                cmd.Parameters.AddWithValue("address", address);
                cmd.ExecuteNonQuery();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a matrix from a file and have the following code: int
I have posted the following code where I am reading from an input file
How I can start reading from a specific byte? I have following code: try
I have the following code reading a file containing unicode text (Japanese). File f
I have the following code that writes structs to a file like so: void
I have the following simple code, that reads contents of a text file into
I have the following basic code to read a text file from a StreamReader:
I have the following code which ends up forever reading '/proc/cpuinfo' as it keeps
I have following code for loading image from url in xml parsing endElement method
I have following code for inserting data into database using PDO. It inserts data

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.