Reputation: 619
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>
Upvotes: 1
Views: 5034
Reputation: 11
total1 ISNULL ?
int iTotal1 = answer = 0;
if(int.TryParse(total1,out iTotal1 ))
{
answer = total2 * Convert.ToInt32(total1);
}else{
// total1 Not Int
}
Upvotes: 1
Reputation: 3963
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).
Upvotes: 2
Reputation: 4278
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!
Upvotes: 1
Reputation: 245459
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"];
Upvotes: 3