2

this is my table:

CREATE TABLE [dbo].[InvoiceTable] (
    [Id]                 INT           IDENTITY (1, 1) NOT NULL,
    [CodeColumn]         NVARCHAR (50) NOT NULL,
    [NameColumn]         NVARCHAR (50) NOT NULL,
    [QTYColumn]          INT           NULL,
    [TotalQTYColumn]     INT           NULL,
    [UnitCostColumn]     INT           NOT NULL,
    [DiscountRateColumn] FLOAT (53)    DEFAULT ((0)) NULL,
    [RowTotalColumn]     AS            (([QTYColumn]*[UnitCostColumn])*((1)-[DiscountRateColumn])) PERSISTED,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

I have an Invoice form and a Confirimation form. User selects the desired goods in the first form and confirms how much he/she should pay in the second form. I want my program to show the value of sum(RowTotalColumn) as a text in my TotalCostTextBox.Text. I wrote the following code:

private int ConfirmForm_Load(object sender, EventArgs e)
{
    string selectQuery = "SELECT Sum(RowTotalColumn) FROM InvoiceTable";
    var sqlcmd = new SqlCommand(selectQuery, sqlcon);
    var result = sqlcmd.ExecuteScalar();
    return int.Parse(result.ToString());
    TotalCostTextBox1.Text = result.ToString();

when I put TotalCostTextBox1.Text = result.ToString(); after return it yells

Unreachable code detected

when I put it before return, TotalCostTextBox1 shows nothing...

Please tell me how to fill the TotalCostTextBox1 with the value. not about return statement.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 2
    because of you use return before TotalCostTextBox1.Text = result.ToString(); when you write return current block close – erdi yılmaz Nov 21 '18 at 09:56
  • This should help solve your problem - https://tutorials.visualstudio.com/vs-get-started/debugging | https://learn.microsoft.com/en-us/visualstudio/debugger/getting-started-with-the-debugger?view=vs-2017 – Rand Random Nov 21 '18 at 10:06
  • Change int to void and remove return after look at your designer.cs is it have code like this `code` this.Load += new System.EventHandler(this.ConfirmForm_Load); `code` is not add this code – erdi yılmaz Nov 21 '18 at 10:31
  • 1
    @erdiyılmaz Thankyou your comment poped something in my head which was related to your comment I fixed it and now my problem is solved. thank you :) – Daniel_Ranjbar Nov 21 '18 at 10:49

3 Answers3

2

The return type of your method is int, this is why you are getting error. You can change your method return type to void and remove the return keyword:

private void ConfirmForm_Load

And

var result = sqlcmd.ExecuteScalar();
int x = int.Parse(result.ToString());
TotalCostTextBox1.Text = x.ToString();
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

Unreachable code detected

Well that's cause you are assigning to your Textbox after the return statement which will never reach. it's a event handler and thus should of void type since event handler doesn't return anything.

private void ConfirmForm_Load(object sender, EventArgs e)
{
    string selectQuery = "SELECT Sum(RowTotalColumn) FROM InvoiceTable";
    var sqlcmd = new SqlCommand(selectQuery, sqlcon);
    var result = sqlcmd.ExecuteScalar();                
    this.TotalCostTextBox1.Text = result.ToString();

Considering TotalCostTextBox1 is the instance name of the TotalCostTextBox1 control

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Rahul
  • 76,197
  • 13
  • 71
  • 125
1

You need to place below assignment statement before return

TotalCostTextBox1.Text = result.ToString();

Your final code should be like below code:

private void ConfirmForm_Load(object sender, EventArgs e)
{
    string selectQuery = "SELECT Sum(RowTotalColumn) FROM InvoiceTable";
    var sqlcmd = new SqlCommand(selectQuery, sqlcon);
    var result = sqlcmd.ExecuteScalar();
    TotalCostTextBox1.Text = result.ToString();
    //return int.Parse(result.ToString());
}

Also you need to check the event binding with ConfirmForm_Load function. To verify this please check if following line is available in ConfirmForm.Designer.cs

this.Load += new System.EventHandler(this.ConfirmForm_Load);