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

  • Home
  • SEARCH
  • 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 8287659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:06:40+00:00 2026-06-08T12:06:40+00:00

Hi guys currently i have this code. I would like to add another field

  • 0

Hi guys currently i have this code. I would like to add another field “GivenName" into the statement but the issue is it is from another table. How do i merge two database tables into this statement without altering the original query output.

namespace Test
{
    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=abcdefg;Initial Catalog=hello;Persist Security Info=True;User ID=sa;Password=");

                         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)
        {

        }
    }
}
  • 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-08T12:06:41+00:00Added an answer on June 8, 2026 at 12:06 pm

    Use a JOIN:

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

    INNER JOIN if it’s a NOT NULL column and LEFT JOIN when the column is NULL.

    So you will add this:

    FROM Invoice inv INNER JOIN
        [YourOtherTable] tab ON inv.[SomeID] = tab.[SomeID]
    

    EDIT:

    Okay, it seems that [MedicalRecordID] is the most obvious.

    FROM [Invoice] inv INNER JOIN 
    [PatientDetails] tab ON inv.[MedicalRecordID] = tab.[MedicalRecordID]
    

    Just remember, if you are looking for a specific PatientDetails record you will need to add another AND to your WHERE clause.

    Something like:

    AND tab.[LastName] = 'SomeLastName'
    

    EDIT:

    SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), inv.[InvDate],3) AS InvDate
                                             , inv.[InvoiceNo]
                                             , inv.[TaxAmount] + inv.[SubTotal] AS Amount
                                             , '' AS Payment 
                                        FROM [Invoice] inv LEFT JOIN 
                                             [PatientDetails] tab ON inv.[MedicalRecordID] = tab.[MedicalRecordID] 
                                        WHERE (inv.[InvDate] >= CONVERT(datetime, '" + dtpFrom.Text + "', 105 )) 
                                        AND (inv.[InvDate] <= CONVERT(datetime, '" + dtpTo.Text + "', 105))", objConn);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys quick question, I currently have an insert statement $query= INSERT into new_mail
Hi Guys I currently have this table MEMBER **ID Position Latitude Longitute** 1 1
guys, I would like to you evaluate next code below. As you see, I
Hey guys I have a query that currently finds the latest comment for each
Hey guys, I currently present my mp3s by referencing their file location into a
Hi guys I am rather new at AJAX programming but I have managed to
I've hit a dead end with this code I'm working on. I have a
Ok guys this makes no sense... I have this method: // break down the
Hi guys I have this script here, which counts the characters and lines, now
Hey guys Im currently trying to debug some code for a mysqli encapsulation class.

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.