0

I want to do this (on Form3):

sqlcon.Open();
            string selectQuery = "SELECT * FROM InvoiceTable where id = @id";
            SqlCommand sqlcmd = new SqlCommand(selectQuery, sqlcon);
            sqldr = sqlcmd.ExecuteReader();
            while(sqldr.Read())
            {
                textbox1 = Max value of TotalCostColumn.value
                or
                textbox1 = Latest value of TotalCostColumn.value
                or
                textbox1 = 2nd row of TotalCostColumn.value
            }

Im not having any datagridview on this form. I do have a datagridview on Form1 which is the actual invoice form. Form3 is the confrimation form which lets the user confirm how much customer should pay eventually. I want the TotalCostColumn's value from my database to be transfered in the related textbox in Form3.

4
  • 2
    Don't execute a SqlReader if you only need one value. Use ExecuteScalar. Also do yourself a favour and call your forms and text boxes some meaningful. Commented Nov 19, 2018 at 9:02
  • My comment is out of topic, but I strongly advise you to correctly name your forms elements. What if you don't write any code for your application for 6 monthes and want to modify it. Will you remember what textbox1, Form1 button42 etc... refers to? Commented Nov 19, 2018 at 9:05
  • @PalleDue thank you for your advice I appreciate it. actually I named them in a meaningfull my self but I thought I have to change those names to these for simplicity Commented Nov 19, 2018 at 15:00
  • @Cid thank you for your advice and your care. I actually changed my real names to these in order them to be simple... I guess I was wrong... thank you Commented Nov 19, 2018 at 15:01

3 Answers 3

2

textbox1 = Max value of TotalCostColumn.value

If that's your requirement then why not build the query as per like

SELECT max(TotalCostColumn) FROM InvoiceTable where id = @id

And then instead of using ExecuteReader() you should call ExecuteScalar() which would return a single value

Sign up to request clarification or add additional context in comments.

Comments

1

This is how you'd get the Max value with ExecuteScalar:

string selectQuery = "SELECT MAX(TotalCostColumn) FROM InvoiceTable where id = @id";
var sqlcmd = new SqlCommand(selectQuery, sqlcon);
var result = sqlcmd.ExecuteScalar();
return int.Parse(result.ToString());

MAX will ensure you only get one row from the table. To get the latest value, you'd have to sort the table (I assume there's a transaction date) and add TOP 1 to get only the first row. (Although ExecuteScalar will ignore everything except the first field of the top row but there's no point returning data you don't need)

There are two ways to get the second row. One is to use a datareader and skip over the first row. The other is with SQL:

SELECT TOP 1 FROM 
(
  SELECT TOP 2 TotalCost FROM InvoiceTable where id = @id ORDER BY TotalCost DESC
) ORDER BY TotalCost ASC

Here the inner query gets two rows of data sorted with the largest item first, and the outer query picks the lower value of the two.

2 Comments

it underlined the result in Parse(result);and threw this error cannot convert from object to string when I used your first code block
@absolute455 - sorry, I've updated my answer to 'int.Parse(result.ToString());' Also, I'm only guessing that you want an 'int' - a cost could be 'decimal'.
0

You need to write a stored procedure which accepts the value of the id

stored procedure is defined as follows

create proc selectmax(@id int)
as begin
select max(TotalCostColumn) FROM InvoiceTable where id = @id
end

and call it as below using Disconnected Model so sqlcon.open() is not Required

SqlCommand sqlcmd = new SqlCommand(selectmax, sqlcon);
sqlcmd.CommandType=CommandType.StoredProcedure
SqlDataAdapter adapter= new SqlDataAdapter(sqlcmd);
      DataSet value=new DataSet();
adapter.Fill(value);

This is the implementation using Ado.net Disconnected Model

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.