简体   繁体   中英

Creating a Table in a RichTextBox C# from Data in my SQL Database

I have done some research about richtextboxes and how to add a table to them. I do not, however, understand how to insert data from my SQL database into this table, I was able to add the needed data to a richtextbox for a preview before sending the text in an email. I tried the above mentioned with some simple code making the output appending the text but this is not sufficient as a large amount of data will be sent via email and makes it hard to read.

Please see my code below (Keep in mind I am very new to the programming world)

SqlConnection connection = new SqlConnection();
        connection.ConnectionString = "Data Source=192.168.1.201;Initial Catalog=JOSKEN;User ID=Admin;Password=Josken123!";
        connection.Open();

        //Reads Information from Database in terms of Job number

        //Variables
        int jobNumber;
        int itemNumber;
        string Qty;
        string itemMake;
        string itemModel;
        string itemSerial;
        string itemType;
        string specs;
        string fault;
        string Assessor;
        string replacementQuote;
        string replacementPrice;
        string totalPrice;
        itemMake = "";
        itemNumber = 0;
        itemType = "";
        itemModel = "";
        itemSerial = "";
        specs = "";
        jobNumber = 0;
        fault = "";
        replacementPrice = "";
        replacementQuote = "";
        totalPrice = "";

        SqlCommand Cmd = new SqlCommand();
        Cmd.Connection = connection;
        string Query = "Select * from Insurance_Items where Job_Number = '" + Convert.ToInt32(metroTxtJobNumberInternalReport.Text) + "'";
        Cmd.CommandText = Query;
        SqlDataReader reader = Cmd.ExecuteReader();
        while (reader.Read())
        {
            for (int n = 1; n <= Convert.ToInt32(metroLblItemNoInternalReport.Text); n++) 
            {
                jobNumber = Convert.ToInt32(reader["Job_Number"]);
                itemNumber = Convert.ToInt32(reader["Item_Number"]);
                Qty = Convert.ToString(reader["Item_Qty"]);
                itemMake = Convert.ToString(reader["Item_Make"]);
                itemModel = Convert.ToString(reader["Item_Model"]);
                itemSerial = Convert.ToString(reader["Item_SerialNumber"]);
                itemType = Convert.ToString(reader["Item_Type"]);
                specs = Convert.ToString(reader["Item_Specs"]);
                fault = Convert.ToString(reader["Item_Fault"]);
                Assessor = Convert.ToString(reader["Assessor_Name"]);
                replacementQuote = Convert.ToString(reader["Replacement_Quote"]);
                replacementPrice = Convert.ToString(reader["Replacement_Price"]);
                totalPrice = Convert.ToString(reader["Total_Price"]);



            }



            string itemText = Environment.NewLine + "Item Make: " + itemMake + " " + itemType + Environment.NewLine + "Model: "
                    + itemModel + Environment.NewLine + "S/N: " + itemSerial
                    + Environment.NewLine + "Specs: " + Environment.NewLine + specs + Environment.NewLine + Environment.NewLine + 
                    "Assessment: " + fault + Environment.NewLine + Environment.NewLine 
                    + "Replacement Quote: " + Environment.NewLine + replacementQuote + Environment.NewLine + "Replacement Price: R" + replacementPrice + Environment.NewLine + "Total Price: R" + totalPrice 
                    + Environment.NewLine + Environment.NewLine;

            RedInternalReport.AppendText(itemText);
            string emailsubject = metrolblClientNameInternalReport.Text + " " + metrolblClaimNumberInternalReport.Text + " " + " | " + " Job Number: " + jobNumber + " | ";
            metroTxtSubjectInternalReport.Text = emailsubject;
            RedInternalReport.AppendText("=======================================================================================================");

        }

        connection.Close();

See below screenshot of the output:

Output of Above Code when sent in email

I would like to output the data into 4 Columns and the number of rows should increase in terms of the number of items I am adding as defined in the metroLblItemNoInternalReport.Text

I would like to add itemMake, itemMode and itemSerial to the first column. I would like to add specs to the second column I would like to add fault to the third column Finally, I would like to add replacementPrice, replacementQuote and totalPrice to the fourth and final column.

Any assistance would be highly appreciated

It would have been better to display your final result in an image. I hope this is what you want.

int jobNumber = 0;
int itemNumber = 0;
string Qty = "Qty";
string itemMake = "itemMake";
string itemModel = "itemModel";
string itemSerial = "itemSerial";
string itemType = "itemType";
string specs = "specs";
string fault = "fault";
string Assessor = "Assessor";
string replacementQuote = "replacementQuote";
string replacementPrice = "replacementPrice";
string totalPrice = "totalPrice";

int maxLen = itemMake.Length;
if (itemModel.Length > maxLen) maxLen = itemModel.Length;
if (itemSerial.Length > maxLen) maxLen = itemSerial.Length;
if (itemType.Length > maxLen) maxLen = itemType.Length;
if (specs.Length > maxLen) maxLen = specs.Length;
if (fault.Length > maxLen) maxLen = fault.Length;
if (replacementQuote.Length > maxLen) maxLen = replacementQuote.Length;
if (replacementPrice.Length > maxLen) maxLen = replacementPrice.Length;
if (totalPrice.Length > maxLen) maxLen = totalPrice.Length;

string formatter = "";
for (int i = 0; i < 4; i++)
{
    formatter += $"{"{"}{i},{-maxLen}{"}"}";
}

// Use for loop to make rows
string formattedText = string.Format(formatter, itemMake, specs, fault, replacementPrice) +
    Environment.NewLine +
    string.Format(formatter, itemModel, "", "", replacementQuote) +
    Environment.NewLine +
    string.Format(formatter, itemSerial, "", "", totalPrice);

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM