Asked by:
HOW TO MULTIPLY DATA FROM DATABASE

Question
-
Hi, may I know how to get data from database and do calculation of ITEMS PRICE and TOTAL PRICE with the QUANTITY filled in the text box.Wednesday, August 12, 2020 5:38 AM
All replies
-
Hi stevenyj90,
>>do calculation of ITEMS PRICE and TOTAL PRICE with the QUANTITY
How do you want to calculate ITEMS PRICE and TOTAL PRICE, could you explain it in detail?
You can use System.Data.SqlClient to connect your database.
And you can use Int32.TryParse method to convert string to int.
The following is the test code you can refer to.SqlDesigner SqlDesigner = new SqlDesigner(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); private void Form1_Load(object sender, EventArgs e) { ds = SqlDesigner.ExecuteDataSet("select ITEMSPRICE, TOTALPRICE from test where id=8 "); dt = ds.Tables[0]; DataRow row = dt.Rows[0]; int ITEMS = 0; Int32.TryParse(row["ITEMSPRICE"].ToString(), out ITEMS); int TOTAL = 0; Int32.TryParse(row["TOTALPRICE"].ToString(), out TOTAL); int boxValue=0; Int32.TryParse(textBox1.Text, out boxValue) } class SqlDesigner { private static string connStr = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog='My Database';Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; public static DataSet ExecuteDataSet(string sql) { using (SqlConnection xonn = new SqlConnection(connStr)) { xonn.Open(); using (SqlCommand cmd = xonn.CreateCommand()) { cmd.CommandText = sql; SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet dataset = new DataSet(); adapter.Fill(dataset); return dataset; } } }
Best Regards,
Daniel Zhang
"Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
For more information, please refer to the sticky post.- Edited by Daniel_Zhang-MSFTMicrosoft contingent staff Wednesday, August 12, 2020 7:45 AM
- Proposed as answer by Daniel_Zhang-MSFTMicrosoft contingent staff Friday, August 28, 2020 1:56 AM
Wednesday, August 12, 2020 7:44 AM -
Something like this would work. Load into a DataTable then do a Sum on the column total.
SELECT productid, dbo.[Order Details].Quantity, FORMAT(SUM(UnitPrice * (1 - Discount) * Quantity), 'N2') AS Total FROM [Order Details] WHERE dbo.[Order Details].OrderID = 10250 GROUP BY ProductID, Quantity;
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
- Proposed as answer by Daniel_Zhang-MSFTMicrosoft contingent staff Friday, August 28, 2020 1:56 AM
Wednesday, August 12, 2020 5:29 PM -
Hi stevenyj90,
Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
Best Regards,
Daniel Zhang"Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
For more information, please refer to the sticky post.Tuesday, August 18, 2020 9:13 AM -
Hi stevenyj90,
How is your problem progressing? If the solution provided solves your problem, please mark it as an answer.
Best Regards,
Daniel Zhang"Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
For more information, please refer to the sticky post.Friday, August 28, 2020 1:56 AM