1

I am working with someone else's code and I'm not good with C# at all!

I need to multiply total1 and total2. The code below gives me answer = 0 and that's not right. What is wrong here?! Also, the value of total1 is saved in a MS SQL database as an int if that matters. When I put in a breakpoint on the last line I notice it's reading total2 as 9 and total1 as "15" (with the quotes).

System.Data.OleDb.OleDbConnection pcn;
System.Data.OleDb.OleDbCommand pcm;
System.Data.OleDb.OleDbDataReader prs;
pcn = new System.Data.OleDb.OleDbConnection("");
pcm = new System.Data.OleDb.OleDbCommand();
pcn.Open();
pcm.Connection = pcn;
var tableSql = String.Format(@"select sum(TicSold) as total1 from BusTickets where TicketType = '9'");
pcm.CommandText = tableSql;
prs = pcm.ExecuteReader();
var rowcount = 0;
while (prs.Read())
{
    rowcount++;
    total1 = prs["total1"].ToString();
}
prs.Close();
pcn.Close();

int total2 = 9;
int answer = total2 * Convert.ToInt32(total1);

UPDATE: I was able to get answer to show the correct number rather than 0 by using Justin's method below, but now I want to show "answer" on the .aspx page. Previously, I would make it public (public int answer) and then in my html put <%= answer %> for the amount to show. But it still shows 0. The code above is my code behind, the code below is my main page. Again, sorry for the lack of knowledge on this, I'm learning.

Total Ticket Profit: <strong><%= answer %></strong></p>
        </div> 
Jesse
  • 8,605
  • 7
  • 47
  • 57
techora
  • 619
  • 3
  • 18
  • 38
  • Can you get the info from the database as an int, as opposed to converting it to string then back again? Does `total1 = prs["total1"]` by itself work? – devilfish17 Apr 17 '13 at 01:00
  • Have you debug your code? what value are you getting in total1 variable – Sachin Apr 17 '13 at 01:00
  • can you check its value on debug?? Then I would also suggest to use `Int32.TryParse` Method instead than using `Convert.ToInt32`. – roybalderama Apr 17 '13 at 01:01
  • @Sachin When I debug I get "15" for total1, thats with quotes around the 15. – techora Apr 17 '13 at 01:15
  • @devilfish17 your right, I should leave it as an Int, my bad! – techora Apr 17 '13 at 01:15
  • 1
    On your second try you can only get 1) an exception for the `int.Parse` 2) a result for total3. How could you get 0 unless total3 or total2 is 0? You said total2 was 9, so if total3 is 15 you can only get 135. – Harrison Apr 17 '13 at 01:41
  • @Harrison Look neither are 0! https://dl.dropboxusercontent.com/u/9446763/code/int2.jpg – techora Apr 17 '13 at 01:58
  • 1
    So after the next line executes answer will be 135, right? What is answer after that line executes? – Harrison Apr 17 '13 at 02:08
  • Yes, it becomes 135, you are right. But when I call it to the front page <%= answer %> it still shows as 0. I have it as public int answer; on code behind – techora Apr 17 '13 at 02:14
  • I'm not sure what you mean. Can you update your question with the relevant code. – Harrison Apr 17 '13 at 02:16
  • @Harrison I updated it, let me know if that helps. I just want to take the answer int from the code behind to the front page now within my HTML. – techora Apr 17 '13 at 02:40
  • see updated **answer** below – Harrison Apr 17 '13 at 03:06

4 Answers4

3

The value should already be an integer coming out of the database so, when reading the value, you should only have to cast the value:

int total1 = (int)prs["total1"];
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • I still get answer = 0 when doing this. The only thing that did change is now during debug the total1 = 15 and not "15". – techora Apr 17 '13 at 01:27
  • I had my breakpoint at the wrong spot, you were right this fixed the equation. Still working on it displaying the number on my front page though. – techora Apr 17 '13 at 02:11
2

Looking at Convert.ToInt32 help page you have the return value shows:

Type: System.Int32
A 32-bit signed integer that is equivalent to the number in value, or 0 (zero) if value is null.

Therefore the "15" that is value for total1 must be returning 0 from the Convert method giving you a total of zero (0) after the multiplication.

I would recommend using

int answer = 0;
int myTotal = 0;

if(int.TryParse(total1, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture,out myTotal))
{
     answer = myTotal * total2;
}

UPDATE

Your updated question looks like it can be answered by looking at this post. It defines the variable as a public property and then calls it similary to how you did <%=answer%>

public string Answer { get { return answer.ToString(); } }

or returning the result of the method you are showing which calculates answer (assuming its return value is answer).

Community
  • 1
  • 1
Harrison
  • 3,843
  • 7
  • 22
  • 49
1

total1 ISNULL ?

int iTotal1 = answer  = 0;
if(int.TryParse(total1,out iTotal1 ))
{
     answer = total2 * Convert.ToInt32(total1);
}else{
    // total1 Not Int
}
Habib
  • 219,104
  • 29
  • 407
  • 436
FREE-SHI
  • 11
  • 2
1

In addition to what others have already asked in the comments to your original question, I'd recommend looking into the ExecuteScalar method instead, as you don't really need to loop through a recordset when only one result is being returned.

But, to answer your question, where is total2 coming from? The variable total1 by itself is certainly going to return a string, since when you set it you are casting the DB result to string. (Where is this variable being declared? e.g., string total1?) Try instead delcaring as int total1 and then when getting the result, total1 = Convert.ToInt32(prs["total1"]);. I hope this helps and good luck to you!

Funka
  • 4,258
  • 2
  • 24
  • 27
  • total2 is always going to be 9. It's just the amount the tickets cost, what I'm trying to do is find how many tickets are in the database then multiply the total sum of tickets by 9. – techora Apr 17 '13 at 01:13
  • Are there are any nulls in the TicSold column? Convert.ToInt32 will change this to zero? – Funka Apr 17 '13 at 01:21
  • There are no nulls in the column there is only two records right now, one with the value of 2 and one with the value of 4. – techora Apr 17 '13 at 01:23
  • Please try declaring `total1` as an int, and lose all the string/ToString cruft, then see if you get a more meaningful error? – Funka Apr 17 '13 at 01:24
  • No luck, still getting 0. hmm https://dl.dropboxusercontent.com/u/9446763/code/int.jpg – techora Apr 17 '13 at 01:30
  • Great picture! But try hovering over both total1 and total2---which of them is the zero? – Funka Apr 17 '13 at 01:41
  • Neither! https://dl.dropboxusercontent.com/u/9446763/code/int2.jpg I'm so confused now! – techora Apr 17 '13 at 01:57
  • 1
    @kcray You're breaking before the result has been assigned to the `answer` variable. Either break on the curly brace, then hover over `answer`, or try hovering over the `*` sign on your current breakpoint. – keyboardP Apr 17 '13 at 02:03
  • @keyboardP Your right, ah man I'm a noob, okay so I moved the break and now answer = 135 like it should. But when I call it on the actual page like <%= answer %> it shows up as 0. Any idea on that? Within the public partial class I state it as public int answer; – techora Apr 17 '13 at 02:10