Export SQL Results To a Three Lines Text File - c#

The Below Code Returns the following data into text file from the database.
Output
InvoiceNo InvoiceDate Amount Vat Total
001 1/1/2018 200 10 210
002 2/1/2018 300 15 315
What i am looking for is to get Amount,Vat and Total in separate line with the same invoice info so Desired out will be like below
Desired out
001 1/1/2018 200
001 1/1/2018 10
001 1/1/2018 210
002 2/1/2018 300
002 2/1/2018 15
002 2/1/2018 315
Code
private void button1_Click(object sender, EventArgs e)
{
string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = #"C:\Log\";
try
{
string FileNamePart = "Invoice";
string DestinationFolder = #"C:\Destination\";
string TableName = "Invoice";
string FileDelimiter = ",";
string FileExtension = ".txt";
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "Data Source = .\\SQLEXPRESS; Initial Catalog =Backoffice; "
+ "Integrated Security=true;";
string query = "Select * From " + TableName;
SqlCommand cmd = new SqlCommand(query, SQLConnection);
SQLConnection.Open();
DataTable d_table = new DataTable();
d_table.Load(cmd.ExecuteReader());
SQLConnection.Close();
string FileFullPath = DestinationFolder + "\\" + FileNamePart + "_" + datetime + FileExtension;
StreamWriter sw = null;
sw = new StreamWriter(FileFullPath, false);
int ColumnCount = d_table.Columns.Count;
for (int ic = 0; ic < ColumnCount; ic++)
{
sw.Write(d_table.Columns[ic]);
if (ic < ColumnCount - 1)
{
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in d_table.Rows)
{
for (int ir = 0; ir < ColumnCount; ir++)
{
if (!Convert.IsDBNull(dr[ir]))
{
sw.Write(dr[ir].ToString());
}
if (ir < ColumnCount - 1)
{
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);
}
sw.Close();
MessageBox.Show("Done..");
}
catch (Exception exception)
{
}
}
How can I archives geting Amount,Vat and Total in separate line with Thanks.

Edit
I was thinking about this answer and I realized I assumed you would want to use this invoice data elsewhere. If you don't need to use the data elsewhere, you can just use this block of code. It doesn't store the data returned from the sql call in an object.
try {
var invoices = new List<Invoice>();
using (var SQLConnection = new SqlConnection(ConnectionString)) {
SQLConnection.Open();
using (var cmd = new SqlCommand(query, SQLConnection))
using (var reader = cmd.ExecuteReader()) {
while (reader.Read()) {
// Note: You should handle nulls if the sql columns are nullable
var number = (int)reader["InvoiceNo"];
var date = (DateTime)reader["InvoiceDate"];
var amount = (int)reader["Amount"];
var vat = (int)reader["Vat"];
var total = (int)reader["Total"];
var iNumAndDate = $"{number}{FileDelimiter}{date.ToString("M/dd/yyyy")}{FileDelimiter}";
sw.Write($"{iNumAndDate}{amount}");
sw.Write($"{iNumAndDate}{vat}");
sw.Write($"{iNumAndDate}{total}");
sw.Write(sw.NewLine);
}
}
}
}
catch (Exception exception) {
// TODO: Handle exceptions
}
Original:
First, I would make a class to store each Invoice, we'll call it Invoice
class Invoice
{
public int Number { get; set; }
public DateTime Date { get; set; }
public int Amount { get; set; }
public int Vat { get; set; }
public int Total { get; set; }
}
Then, just store your data from the dB in a List<Invoice> and then loop through this list to write to the file.
try {
var invoices = new List<Invoice>();
using (var SQLConnection = new SqlConnection(ConnectionString))
{
SQLConnection.Open();
using (var cmd = new SqlCommand(query, SQLConnection))
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
invoices.Add(new Invoice {
// Note: You should handle nulls if the sql columns are nullable
Number = (int)reader["InvoiceNo"],
Date = (DateTime)reader["InvoiceDate"],
Amount = (int)reader["Amount"],
Vat = (int)reader["Vat"],
Total = (int)reader["Total"]
});
}
}
}
using (sw = new StreamWriter(FileFullPath, false))
{
foreach (var invoice in invoices)
{
var iNumAndDate = $"{invoice.Number}{FileDelimiter}{invoice.Date.ToString("M/dd/yyyy")}{FileDelimiter}";
sw.Write($"{iNumAndDate}{invoice.Amount}");
sw.Write($"{iNumAndDate}{invoice.Vat}");
sw.Write($"{iNumAndDate}{invoice.Total}");
sw.Write(sw.NewLine);
}
}
}
catch (Exception exception)
{
// TODO: Handle exceptions
}

You could do something like this :
foreach (DataRow dr in d_table.Rows)
{
StringBuilder builder = new StringBuilder();
for (int ir = 0; ir < 3; ir++)
{
if (!Convert.IsDBNull(dr[ir]))
{
builder.Append(dr[ir].ToString());
}
builder.Append(FileDelimiter);
}
var lineStart = builder.ToString();
for (int ir = 3; ir < 6; ir++)
{
if (!Convert.IsDBNull(dr[ir]))
{
sw.Write(lineStart);
sw.Write(dr[ir].ToString());
sw.Write(sw.NewLine);
}
}
}

Related

Recommended BatchSize for dealing with total x milion transaction

Goal:
Handle a big batchsize based on total 3 million transaction (at least 3 milion) without any error or bug.
Problem:
How much can batchsize contain in order make a transaction based on total transaction of 3 million?
I don't want any bug or similiar during the process time of dealing with total of 3 milion transaction.
Info:
*The code is a sample and in reality the size is much bigger.
*The data is dummy.
*The total transaction can be larger size than 3 milion.
Thank you!
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApp9
{
public static class Program
{
private static int _loop = 20000; // 20 000 transaction
static void Main(string[] args)
{
List<Test> listOfTest = new List<Test>();
for (int i = 0; i < _loop; i++)
{
var price = NextDecimal(new Random());
var rank = new Random().Next(int.MinValue, int.MaxValue);
Test test = new Test(price, rank);
listOfTest.Add(test);
}
InsertData(listOfTest);
}
public static string GetConnectionString()
{
return
"Data Source=" +
"Initial Catalog=Test;" +
"Integrated Security=SSPI;";
}
private static void InsertData(List<Test> listOfTest)
{
const string queryConst = "INSERT INTO dbo.TestTbl (Price, Rank) " +
"VALUES ";
const int batchMaxSize = 5;
string query = queryConst;
var sep = "";
var batchSize = 0;
using (SqlConnection cn = new SqlConnection(GetConnectionString()))
using (SqlCommand cmd = new SqlCommand("", cn))
{
try
{
cn.Open();
for (int i = 0; i < listOfTest.Count; i++)
{
batchSize++;
query += $#"{sep}(#Price{i}, #Rank{i})";
cmd.Parameters.Add("#Price" + i.ToString(), SqlDbType.Decimal).Value = listOfTest[i].Price;
cmd.Parameters.Add("#Rank" + i.ToString(), SqlDbType.Int).Value = listOfTest[i].Rank;
sep = ",";
if ((batchSize == batchMaxSize) || ((i + 1) == listOfTest.Count))
{
cmd.CommandText = query;
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
batchSize = 0;
query = queryConst;
sep = "";
}
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
cn.Close();
}
}
}
public static decimal NextDecimal(this Random rng)
{
byte scale = (byte)rng.Next(29);
bool sign = rng.Next(2) == 1;
return new decimal(rng.NextInt32(),
rng.NextInt32(),
rng.NextInt32(),
sign,
scale);
}
public static int NextInt32(this Random rng)
{
int firstBits = rng.Next(0, 1 << 4) << 28;
int lastBits = rng.Next(0, 1 << 28);
return firstBits | lastBits;
}
}
public class Test
{
public Test(decimal price, int rank)
{
this.Price = price;
this.Rank = rank;
}
public decimal Price{ get; set; }
public int Rank { get; set; }
}
}
CREATE TABLE [dbo].[TestTbl]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[Price] [decimal](38, 5) NULL,
[Rank] [int] NULL
)

How to query a C# list against a database table

My code isn't returning any rows from a test database table when I pass a string version of a list, but it does return rows if I pass the list members in directly.
When I use a message box to show the string joinedSerialsList, it appears to be formatted properly.
// Create comma delimited list of serials:
int currentSerial = beginning;
List<string> serialsList = new List<string>();
for (int i = 0; i < count; i++)
{
serialsList.Add(currentSerial.ToString());
currentSerial++;
}
string joinedSerialsList = string.Format("({0})", string.Join(", ", serialsList));
OleDbConnection connection = BadgeDatabaseDB.GetConnection();
string checkStatement
= "SELECT SerialNumber, OrderNumber "
+ "FROM SerialNumbersMFG "
+ "WHERE SerialNumber IN (#List)";
OleDbCommand command = new OleDbCommand(checkStatement, connection);
command.Parameters.AddWithValue("#List", joinedSerialsList);
string duplicateSerials = "";
try
{
connection.Open();
OleDbDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
duplicateSerials += dataReader["OrderNumber"].ToString() + "\n";
}
}
catch (OleDbException ex)
{
throw ex;
}
finally
{
connection.Close();
}
return duplicateSerials;
I rewrited your sample, this work:
private IEnumerable<string> getData()
{
// Create comma delimited list of serials:
int currentSerial = 4452; // your constant
var serialsList = new List<int>();
var count = 100;
for (int i = 0; i < count; i++)
serialsList.Add(currentSerial++);
var connString = getConnectionString();
var results = new List<string>();
string sqlSelect = $"SELECT SerialNumber, OrderNumber FROM SerialNumbersMFG WHERE SerialNumber IN ({string.Join(",", serialsList)})";
using (var connection = new SqlConnection(connString)) // BadgeDatabaseDB.GetConnection();
{
using (var command = new SqlCommand(sqlSelect, connection))
{
connection.Open();
var dataReader = command.ExecuteReader();
while (dataReader.Read())
results.Add(dataReader["OrderNumber"].ToString());
}
}
return results;
}

Retrieve multiple rows from SQL Server in a web service and xamarin android?

I want to retrieve multiple rows from SQL Server in a web service. I get 2 rows as result when I search jo number 1 and my table name is TestOrderStatus.
While I get only one row in the search result.
Thanks for all
public class ReturnOrder
{
public string Message;
public int QtqSlit;
public int QtyPcs;
public string Design;
}
[WebMethod(MessageName = "OrderStatus", Description = "OrderStatus new Order")]
[System.Xml.Serialization.XmlInclude(typeof(ReturnOrder))]
public ReturnOrder OrderStatus(string JO) /// get list of notes
{
int QtqSlit = 0;
int QtyPcs = 0;
String Design = "";
string Message = "";
SqlDataReader reader;
using (SqlConnection connection = new SqlConnection(DBConnection.ConnectionString))
{
SqlCommand cmd = new SqlCommand("SELECT QtqSlit,QtyPcs,Design FROM TestOrderStatus where JO=#JO");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("#JO", JO);
connection.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
QtqSlit = reader.GetInt32(0);
QtyPcs = reader.GetInt32(1);
Design = reader.GetString(2);
}
}
if (QtqSlit == 0)
{
Message = " user name or password is incorrect";
}
reader.Close();
connection.Close();
}
ReturnOrder rt = new ReturnOrder();
rt.Message = Message;
rt.QtqSlit = QtqSlit;
rt.QtyPcs = QtyPcs;
rt.Design = Design;
return rt;
}
You need a List for multiple rows if you want more than one result. So your code may be like this.
public class ReturnOrder
{
public string Message;
public int QtqSlit;
public int QtyPcs;
public string Design;
}
[WebMethod(MessageName = "OrderStatus", Description = "OrderStatus new Order")]
[System.Xml.Serialization.XmlInclude(typeof(List<ReturnOrder>))]
public List<ReturnOrder> OrderStatus(string JO) /// get list of notes
{
List<ReturnOrder> result=new List<ReturnOrder>();
int QtqSlit = 0;
int QtyPcs = 0;
String Design = "";
string Message = "";
//try
//{
SqlDataReader reader;
using (SqlConnection connection = new SqlConnection(DBConnection.ConnectionString))
{
SqlCommand cmd = new SqlCommand("SELECT QtqSlit,QtyPcs,Design FROM TestOrderStatus where JO=#JO");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("#JO", JO);
connection.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
QtqSlit = reader.GetInt32(0);
QtyPcs = reader.GetInt32(1);
Design = reader.GetString(2);
ReturnOrder rt = new ReturnOrder();
rt.Message = Message;
rt.QtqSlit = QtqSlit;
rt.QtyPcs = QtyPcs;
rt.Design = Design;
result.add(rt);
}
if (QtqSlit == 0)
{
Message = " user name or password is in correct";
}
reader.Close();
connection.Close();
}
//}
//catch (Exception ex)
//{
// Message = " cannot access to the data";
//}
return result;
}
Thank you to much now web service is ok , But when he summoned the values in Xamrian Android doesn't know.
public class Mainlistview : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Mainlistview);
ListView ListView = FindViewById<ListView>(Resource.Id.listView1);
Selling.WebServiceDB ws = new Selling.WebServiceDB();
ws.OrderStatusListCompleted += Ws_OrderStatusListCompleted;
ws.OrderStatusListAsync(Convert.ToString(1));
}
private void Ws_OrderStatusListCompleted(object sender, Selling.OrderStatusListCompletedEventArgs e)
{
ListView ListView = FindViewById<ListView>(Resource.Id.listView1);
string msg = "";
if (e.Result.QtqSlit.ToString().Equals("0"))
{
msg = e.Result.Message;
}
else
{
// full class
List<TableItem> tableItems = new List<TableItem>();
tableItems.Add(new TableItem("" + e.Result.QtqSlit, "" + e.Result.QtyPcs, Resource.Drawable.Icon));
ListView.Adapter = new HomeScreenAdapter(this, tableItems);
}
}

The connection is open

A few months ago I made a test program for a project and everything worked fine there.
Now I am working on the program itself, so I copied the code from the test program and
changed the the names of the columns,buttons etc. so it would fit the current program.
When I try to add something into the database it does nothing on the first click, on the
second pops up an error which says that the connection is open.. I really got no idea what's
the problem. I tried to check again if I made a mistake in a column name or the database name
but everything seems to be correct.
Note: I also have a function that show data from the database and it works without any problem.
private void InsertData()
{
string NewCode = GenerateCode();
string NewSentence = txtSentence.Text;
string NewRow = NewRowNum();
try
{
string AddData = "INSERT INTO ShopSentences (BinaryStrings,Sentence,RowNumber) VALUES (#NewBinaryString,#NewSentence,#NewRowNumber)";
SqlCommand DataAdd = new SqlCommand(AddData, Connection);
DataAdd.Parameters.AddWithValue("#NewBinaryString", NewCode);
DataAdd.Parameters.AddWithValue("#NewNewSentence", NewSentence);
DataAdd.Parameters.AddWithValue("#NewRowNumber", NewRow);
Connection.Open();
DataAdd.ExecuteNonQuery();
Connection.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
//Checking the banary code in the last row
string GenerateCode()
{
string RowNo = RowFind();
int Row = int.Parse(RowNo);
int Code = Row + 1;
string Cd = Convert.ToString(Code, 2);
int Ln = Cd.Trim().Length;
if (Ln == 3)
{
Cd = "100" + Cd;
}
else if (Ln == 4)
{
Cd = "10" + Cd;
}
else if (Ln == 5)
{
Cd = "1" + Cd;
}
return Cd;
}
//Finding the last row
string RowFind()
{
Connection.Open();
string queryString = string.Format("SELECT * FROM ShopSentences");
SqlDataAdapter sda = new SqlDataAdapter(queryString, Connection);
DataTable dt = new DataTable("ShopSentences");
sda.Fill(dt);
Connection.Close();
return dt.Rows[dt.Rows.Count - 1]["RowNumber"].ToString();
}
string NewRowNum()
{
string Row = RowFind();
int CalcRow = int.Parse(Row) + 1;
Row = CalcRow.ToString();
return Row;
}
The connection that appears to be open is the one in the string RowFind().
Here are the other related things to the database:
public partial class frmShop : Form
{
System.Data.SqlClient.SqlConnection Connection;
public frmShop()
{
string DatabaseConnection = WindowsFormsApplication1.Properties.Settings.Default.BinaryStringsDictionaryConnectionString1;
Connection = new System.Data.SqlClient.SqlConnection();
Connection.ConnectionString = DatabaseConnection;
InitializeComponent();
}
private void frmShop_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'binaryStringsDictionaryDataSet.ShopSentences' table. You can move, or remove it, as needed.
this.shopSentencesTableAdapter.Fill(this.binaryStringsDictionaryDataSet.ShopSentences);
}
private void GetSentence()
{
try
{
Connection.Open();
SqlDataReader ReadSentence = null;
Int32 BinaryInt = Int32.Parse(txtBinaryString.Text);
string CommandString = "SELECT Sentence FROM ShopSentences WHERE BinaryStrings = #BinaryString";
SqlCommand Command = new SqlCommand(CommandString, Connection);
Command.Parameters.Add("#BinaryString", System.Data.SqlDbType.Int).Value = BinaryInt;
ReadSentence = Command.ExecuteReader();
while (ReadSentence.Read())
{
txtSentence.Text = (ReadSentence["Sentence"].ToString());
Fit = 1;
}
Connection.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
You are getting errors because you are reusing the same connection Connection.Open(); several times.
Your method InsertData() is doing this 3 times in the same method.
You should create a new instance of the connection object and dispose it on your methods.
Using Statement are the way to go.
private void InsertData()
{
using (var Connection = new SqlConnection(DatabaseConnection))
{
string NewCode = GenerateCode();
string NewSentence = txtSentence.Text;
string NewRow = NewRowNum();
try
{
Connection.Open();
string AddData = "INSERT INTO ShopSentences (BinaryStrings,Sentence,RowNumber) VALUES (#NewBinaryString,#NewSentence,#NewRowNumber)";
SqlCommand DataAdd = new SqlCommand(AddData, Connection);
DataAdd.Parameters.AddWithValue("#NewBinaryString", NewCode);
DataAdd.Parameters.AddWithValue("#NewNewSentence", NewSentence);
DataAdd.Parameters.AddWithValue("#NewRowNumber", NewRow);
DataAdd.ExecuteNonQuery();
//Connection.Close(); no need to close
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
You can save one more connection if you store the row returned by RowFind()
string RowFind()
{
using (var Connection = new SqlConnection(DatabaseConnection))
{
Connection.Open();
string queryString = string.Format("SELECT * FROM ShopSentences");
SqlDataAdapter sda = new SqlDataAdapter(queryString, Connection);
DataTable dt = new DataTable("ShopSentences");
sda.Fill(dt);
//Connection.Close();
return dt.Rows[dt.Rows.Count - 1]["RowNumber"].ToString();
}
}
So you would connect once instead of twice:
var Row = RowFind();
string NewCode = GenerateCode(Row);
string NewRow = NewRowNum(Row);
string NewSentence = txtSentence.Text;
Declare your connection string variable to a property so you can reuse it:
private string DatabaseConnection {get; set;}
Instead using an instance level SqlConnection you should only provide a common factory for creating a connection:
public partial class frmShop : Form
{
private string ConnectionString
{
get { return WindowsFormsApplication1.Properties.Settings.Default.BinaryStringsDictionaryConnectionString1; }
}
public frmShop()
{
InitializeComponent();
}
private SqlConnection CreateConnection()
{
var conn = new SqlConnection(ConnectionString);
conn.Open();
return conn;
}
private void frmShop_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'binaryStringsDictionaryDataSet.ShopSentences' table. You can move, or remove it, as needed.
this.shopSentencesTableAdapter.Fill(this.binaryStringsDictionaryDataSet.ShopSentences);
}
private void GetSentence()
{
try
{
using (var conn = CreateConnection())
{
var BinaryInt = int.Parse(txtBinaryString.Text);
var commandString = "SELECT Sentence FROM ShopSentences WHERE BinaryStrings = #BinaryString";
using (var Command = new SqlCommand(commandString, conn))
{
Command.Parameters.Add("#BinaryString", System.Data.SqlDbType.Int).Value = BinaryInt;
using (var readSentence = Command.ExecuteReader())
{
while (readSentence.Read())
{
txtSentence.Text = (readSentence["Sentence"].ToString());
Fit = 1;
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private void InsertData()
{
try
{
using (var conn = CreateConnection())
{
var commandString = "INSERT INTO ShopSentences (BinaryStrings,Sentence,RowNumber) VALUES (#NewBinaryString,#NewSentence,#NewRowNumber)";
using (var comm = new SqlCommand(commandString, conn))
{
comm.Parameters.AddWithValue("#NewBinaryString", GenerateCode());
comm.Parameters.AddWithValue("#NewNewSentence", txtSentence.Text);
comm.Parameters.AddWithValue("#NewRowNumber", NewRowNum());
comm.ExecuteNonQuery();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
//Checking the banary code in the last row
string GenerateCode()
{
string RowNo = RowFind();
int Row = int.Parse(RowNo);
int Code = Row + 1;
string Cd = Convert.ToString(Code, 2);
int Ln = Cd.Trim().Length;
if (Ln == 3)
{
Cd = "100" + Cd;
}
else if (Ln == 4)
{
Cd = "10" + Cd;
}
else if (Ln == 5)
{
Cd = "1" + Cd;
}
return Cd;
}
//Finding the last row
string RowFind()
{
using (var conn = CreateConnection())
{
var commandString = "SELECT * FROM ShopSentences";
using (var comm = new SqlCommand(commandString, conn))
{
using (var sda = new SqlDataAdapter(queryString, Connection))
{
using (DataTable dt = new DataTable("ShopSentences"))
{
sda.Fill(dt);
return dt.Rows[dt.Rows.Count - 1]["RowNumber"].ToString();
}
}
}
}
}
string NewRowNum()
{
var Row = RowFind();
var CalcRow = int.Parse(Row) + 1;
return CalcRow.ToString();
}
}
But this is just the beginning you should not have any hard SQL dependency in your Form classes.
When sharing same SqlConnection instance several times in your code, instead of directly opening the you can rather check the connection state first and then open it if not already opened. For example:
if(Connection.State!= ConnectionState.Open)
Connection.Open();

Is my logic correct? I'm trying to calculate the total amount of each loop that's being read in a foreach loop?

So I grab each row in a foreach loop and calculate the price of it. I managed to do that but I can't seems to figure out how to store ALL the calculated rows into a single variable and insert 1 calculated answer into a database.
private void btnSubmitConsultation_Click(object sender, EventArgs e)
{
string cMedication = string.Empty;
string cQuantity = string.Empty;
string cAppointment = string.Empty;
foreach (DataGridViewRow row in this.dataPrescription.Rows)
{
cMedication = row.Cells[0].Value.ToString();
cQuantity = row.Cells[1].Value.ToString();
cAppointment = txtAppointmentID.Text;
if (cAppointment == "NO APPOINTMENT HAS BEEN MADE")
{
MessageBox.Show("Please make an appointment first at the Nurse counter", "WARNING");
}
else
{
this.calculateTotal(cMedication, cQuantity, cAppointment);
}
}
}
private void calculateTotal(string cMedication, string cQuantity, string cAppointment)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["HConnection"].ConnectionString;
SqlConnection con = new SqlConnection(strConnectionString);
string insertPayment = "INSERT INTO PAYMENT (amount, appointmentID) " +
"VALUES (#insertAmount, #insertAppointment)";
using (SqlConnection connection = new SqlConnection(strConnectionString))
{
using (SqlCommand cmdPayment = new SqlCommand(insertPayment, connection))
{
string strPrice = "SELECT medicationPrice FROM MEDICATION WHERE medicationName= #getName";
SqlCommand cmdPrice = new SqlCommand(strPrice, con);
cmdPrice.Parameters.AddWithValue("#getName", cMedication);
con.Open();
SqlDataReader readPrice = cmdPrice.ExecuteReader();
if (readPrice.Read())
{
string getPrice = readPrice["medicationPrice"].ToString();
double doublePrice = Convert.ToDouble(getPrice);
double doubleQuantity = Convert.ToDouble(cQuantity);
double result = doublePrice * doubleQuantity;
for (int i = 0; i < dataPrescription.Rows.Count; i++)
{
double total = result * i;
string answer = result.ToString();
MessageBox.Show(answer);
cmdPayment.Parameters.AddWithValue("#insertAmount", answer);
}
}
readPrice.Close();
con.Close();
cmdPayment.Parameters.AddWithValue("#insertAppointment", txtAppointmentID.Text);
connection.Open();
cmdPayment.ExecuteNonQuery();
connection.Close();
}
}
}
You have to change the command text to be the insert statement.
connection.Open();
cmdPayment.CommandText = insertPayment;
cmdPayment.ExexuteNonQuerry();

Categories

Resources