Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Textbox7.Text = "ISNULL(SUM(TRANSACTIONDETAILS.BFAST),0)  AS BREAKFAST_COUNT,(ISNULL(SUM(TRANSACTIONDETAILS.BFASTA1),0)+ISNULL(SUM(TRANSACTIONDETAILS.BFASTA2),0)+ISNULL(SUM(TRANSACTIONDETAILS.BFASTA3),0))/NULLif(SUM(TRANSACTIONDETAILS.BFAST),0) AS B_COST,ISNULL(SUM(TRANSACTIONDETAILS.BFASTA1),0)+ISNULL(SUM(TRANSACTIONDETAILS.BFASTA2),0)+ISNULL(SUM(TRANSACTIONDETAILS.BFASTA3),0) AS B_AMOUNT, ISNULL(SUM(TRANSACTIONDETAILS.LUNCH),0) as LUCNH_COUNT,(ISNULL(SUM(TRANSACTIONDETAILS.LUNCHA1),0)+ISNULL(SUM(TRANSACTIONDETAILS.LUNCHA2),0)+ISNULL(SUM(TRANSACTIONDETAILS.LUNCHA3),0))/NULLif(SUM(TRANSACTIONDETAILS.LUNCH),0)  as L_COST, ISNULL(SUM(TRANSACTIONDETAILS.LUNCHA1),0)+ ISNULL(SUM(TRANSACTIONDETAILS.LUNCHA2),0)+ ISNULL(SUM(TRANSACTIONDETAILS.LUNCHA3),0) as L_AMOUNT, ISNULL(SUM(TRANSACTIONDETAILS.DINNER),0)  AS DINNER_COUNT,(ISNULL(SUM(TRANSACTIONDETAILS.DINNERA1),0)+ISNULL(SUM(TRANSACTIONDETAILS.DINNERA2),0)+ISNULL(SUM(TRANSACTIONDETAILS.DINNERA3),0))/NULLif(SUM(TRANSACTIONDETAILS.DINNER),0) AS D_COST,ISNULL(SUM(TRANSACTIONDETAILS.DINNERA1),0)+ISNULL(SUM(TRANSACTIONDETAILS.DINNERA2),0) +ISNULL(SUM(TRANSACTIONDETAILS.DINNERA3),0) AS D_AMOUNT,ISNULL(SUM(TRANSACTIONDETAILS.BFASTA1),0)+ISNULL(SUM(TRANSACTIONDETAILS.LUNCHA1),0) +ISNULL(SUM(TRANSACTIONDETAILS.DINNERA1),0) AS total"


What I have tried:

I wrote a lot of code and it faile

I need to show the sum from the datagrid to the label

I hope for a solution

Thanks to all
Posted

That's part of an SQL command, but as far as the Textbox is concerned, it's just a string, so it will display it.
TextBoxes don't process SQL, don't "magically know" about your database, and won't do arithmetic.

Even is they did, that fragment describes multiple columns in an SQL Command, so the text box would have no idea which of them to display anyway!

I'd suggest you go back to where you got the fragment from, and look at teh code around it: There will be "support code" which connects to a DB and prepares the SQL command before actually executing the query and handling the results.

Quote:
Do you mean this code?
......................................
VB
Dim adapter3 As New SqlDataAdapter("SELECT BUILDING.Location,Mtype as MealType, " & Textbox7.Text & " FROM TRANSACTIONDETAILS INNER JOIN building on TRANSACTIONDETAILS.Bno=building.Bno WHERE " & Textbox6.Text & " TRANSACTIONDETAILS.TRANSACTIONDATE >= '" & RDate & "' and TRANSACTIONDETAILS.TRANSACTIONDATE <= '" & CCDate & "' GROUP BY BUILDING.Location,Mtype ORDER BY BUILDING.Location", Connection)
Dim ds3 As New DataSet()
adapter3.Fill(ds3, "tran3")
Datagrid2.DataSource = ds3.Tables("tran3").DefaultView
Datagrid2.DataBind()
That's part of it, yes - it sets up a DataAdapter (which is one of two ways to read multiple rows
and / or columns info from a DB in VB) with an existing SqlConnection, then reads the data from SQL Server into a DataSet and uses that to fill a DataGrid with the data.

That's how it works: you create a SqlConnection, you set up an SqlCommand (which is done for you as part of the DataAdapter creation), then you tell SQL Server to execute the command, and you handle the results it returns.

But that code does it in a very dangerous way!
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
v2
Comments
yossef2023 15-Oct-23 1:29am    
Do you mean this code?
......................................
Dim adapter3 As New SqlDataAdapter("SELECT BUILDING.Location,Mtype as MealType, " & Textbox7.Text & " FROM TRANSACTIONDETAILS INNER JOIN building on TRANSACTIONDETAILS.Bno=building.Bno WHERE " & Textbox6.Text & " TRANSACTIONDETAILS.TRANSACTIONDATE >= '" & RDate & "' and TRANSACTIONDETAILS.TRANSACTIONDATE <= '" & CCDate & "' GROUP BY BUILDING.Location,Mtype ORDER BY BUILDING.Location", Connection)
Dim ds3 As New DataSet()
adapter3.Fill(ds3, "tran3")
Datagrid2.DataSource = ds3.Tables("tran3").DefaultView
Datagrid2.DataBind()
OriginalGriff 15-Oct-23 1:57am    
Answer updated.
Wow. So many issues in a single statement.

For starters, the code you posted makes no mention of a Label control.

You didn't write much code at all. All you did was set the Text of a TextBox control to a LONG string that looks like partial SQL. The Text property will NOT execute anything at all. It does not understand what you assign to the Text property should be a filter.

What you're trying to filter, or otherwise process, looks like it should be part of an SQL SELECT query.

All of the SQL that you did post, suggests you don't understand much of SQL and your database appears to have major design flaws. You're not normalizing the data that ends up in the database. Read What is Normalization in SQL? 1NF, 2NF, 3NF and BCNF | Simplilearn[^] for a brief overview of what that means.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900