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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:46:58+00:00 2026-06-12T12:46:58+00:00

DISCLAIMER : THIS IS A TEST/DUMMY/FAKE DATABASE Hi guys, i have a problem, below

  • 0

DISCLAIMER : THIS IS A TEST/DUMMY/FAKE DATABASE

Hi guys, i have a problem, below are the 2 table structures. When i use

SELECT CONVERT(char(80), InvDate,3) AS InvDate,InvoiceNo,EmployerCode,TaxAmount + SubTotal AS Amount,'' AS Payment FROM dbo.Invoice;

enter image description here

I wish to add in a column patients name where by it will be tagged to invoice number. So what i mean is that, when the query is executed, it should show me the patientdetails tagged together with invoice number. But in the both table structures there are no links. The only linkage i can think of “MedicalRecordID”. I’m tried using UNION function didnt give me the desired output. Any help?

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;
using System.Data.SqlClient;


namespace MedicalDataExporter
{
public partial class frmSales : Form
{
    public frmSales()
    {
        InitializeComponent();
    }


    private void dtpFrom_ValueChanged(object sender, EventArgs e)
    {

    }

    private void btnExtract_Click(object sender, EventArgs e)
    {

        SqlConnection objConn = new SqlConnection("Data Source=test;Initial Catalog=test;Persist Security Info=True;User ID=test;Password=test");

       System.Data.SqlClient.SqlConnection(conStr);
        objConn.Open();

        SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), InvDate,3) AS InvDate,InvoiceNo,EmployerCode,TaxAmount + SubTotal AS Amount,'' AS Payment FROM Invoice WHERE (InvDate >= CONVERT(datetime, '"+dtpFrom.Text +"', 105 )) AND (InvDate <= CONVERT(datetime, '"+dtpTo.Text+"', 105))", objConn);

        SqlDataReader objReader;
        objReader = objCmd.ExecuteReader();

        System.IO.FileStream fs = new System.IO.FileStream("C:\\CMSExportedData\\Sales-" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", System.IO.FileMode.Create);
        System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);

        int count = 0;
        while (objReader.Read())
        {

            for (int i = 0; i < 5; i++)
            {
                if (!objReader.IsDBNull(i))
                {
                    string s;
                    s = objReader.GetDataTypeName(i);
                    //MessageBox.Show(s);
                    if (objReader.GetDataTypeName(i) == "char")
                    {
                        sw.Write(objReader.GetString(i));
                    }
                    else if (objReader.GetDataTypeName(i) == "money")

                    {
                        sw.Write(objReader.GetSqlMoney(i).ToString());
                    }
                    else if (objReader.GetDataTypeName(i) == "nvarchar")
                    {
                        sw.Write(objReader.GetString(i));
                    }
                }
                if (i < 4)
                {
                    sw.Write("\t");
                }

            }
            count = count + 1;
            sw.WriteLine();

        }
        sw.Flush();
        fs.Close();
        objReader.Close();
        objConn.Close();
        MessageBox.Show(count + " records exported successfully.");
        this.Close();
    }

    private void groupBox1_Enter(object sender, EventArgs e)
    {

    }

    private void dtpTo_ValueChanged(object sender, EventArgs e)
    {

    }

    private void frmSales_Load(object sender, EventArgs e)
    {

    }
}
}

Here is the table structure:

enter image description here

Here is the 2nd table structure:

enter image description here

  • 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-12T12:47:00+00:00Added an answer on June 12, 2026 at 12:47 pm

    To query data across multiple tables, you want to join the tables. I’m not 100% clear on the relationship between your two tables, but if MedicalRecordID is the correct relationship, then your query should look something like this:

    SELECT
        CONVERT(char(80), i.InvDate,3) AS InvDate,
        i.InvoiceNo,
        i.EmployerCode,
        i.TaxAmount + i.SubTotal AS Amount,
        '' AS Payment,
        pd.LastName,
        pd.GivenName
    FROM
        dbo.Invoice i
            INNER JOIN dbo.PatientDetails pd ON (pd.MedicalRecordID = i.MedicalRecordID)
    ;
    

    This works if there is a one-to-one relationship between tables, and if there is always a PatientDetails record for each invoice. If PatientDetails is optional, then use LEFT JOIN instead of INNER JOIN.

    EDIT (response to comment):

    I’m betting that the DateTime conversion in your WHERE clause is not working the way you expect. Assuming that dtpFrom and dtpTo are DatePicker controls, you probably want to use the SelectedDate property instead of Text. Also, I would highly recommend using parameters in your queries rather than concatenating strings. Your code will be cleaner, and you’ll avoid SQL injection. Here’s a quick example:

    using (SqlConnection connection = new SqlConnection( ... ))
    {
        connection.Open();
    
        string sql = @"
                    SELECT
                        CONVERT(char(80), i.InvDate,3) AS InvDate,
                        i.InvoiceNo,
                        i.EmployerCode,
                        i.TaxAmount + i.SubTotal AS Amount,
                        '' AS Payment,
                        pd.GivenName
                    FROM
                        dbo.Invoice i
                            LEFT JOIN dbo.PatientDetails pd ON (pd.MedicalRecordID = i.MedicalRecordID)
                    WHERE
                        InvDate >= @fromDate AND InvDate <= @toDate";
    
        SqlCommand cmd = new SqlCommand(sql, connection);
        cmd.Parameters.AddWithValue("@fromDate", dtpFrom.SelectedDate);
        cmd.Parameters.AddWithValue("@toDate", dtpTo.SelectedDate);
    
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            // do stuff with results
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Disclaimer: I realize this is a massive wall of text, but I have done
The Disclaimer First of all, I know this question (or close variations) have been
( Disclaimer: This question is not specific to ASP.NET) I have a control which
Disclaimer: this question is purely informational and does not represent an actual problem I'm
Disclaimer: i have searched genericly (Google) and here for some information on this topic,
Disclaimer: this is a (frustrating) homework related problem. I'm having odd results when I
Disclaimer This is not a question about whether we should be escaping for database
Disclaimer: this question is strictly about mysql and database abstraction layers that support it.
Disclaimer: I have looked through this question and this question but they both got
Disclaimer: this question is driven by my personal curiosity more than an actual need

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.