Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is the code of
"
VB
Private Sub GetQuantity()
       Try
           sqL = "SELECT StocksOnhand FROM Item WHERE ItemNo = " & txtItemNo.Text & ""
           ConnDB()
           cmd = New OleDbCommand(sqL, conn)
           dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
           If dr.Read = True Then
               getStocksOnHand = dr(0)


           End If
       Catch ex As Exception
           MsgBox(ex.Message)
       Finally
           cmd.Dispose()
           conn.Close()
       End Try
   End Sub

"

i want to get this code into in c# but in the line getStocksOnHand=dr(0);
there is the problem that " can not implicitly convert string to int.
how to solve it
Posted

1 solution

Indexer in C# is the bracket, not the parenthesis. Moreover, you seem to try to affect a string variable to an integer variable.
Thus, said line should be
C#
getStocksOnHand = int.Parse(dr[0].ToString());

Or a more robust way that would take care about wrong values:
C#
int value;
if (int.TryParse(dr[0].ToString(), out value)) {
   getStocksOnHand = value;
   // ...
}


Hope this helps.
 
Share this answer
 
v3
Comments
Member 10720633 11-Apr-14 3:43am    
i use upper code but error is that " the best overloaded method match for int.parse(string) has some invalid arguments
if (dr.Read == true)
{
getStocksOnHand = int.Parse(dr[0]);
}
Tom Marvolo Riddle 11-Apr-14 3:56am    
Try this:

getStocksOnHand = int.Parse(dr[0].ToString());
phil.o 11-Apr-14 3:58am    
Thank you for correcting :)
Tom Marvolo Riddle 11-Apr-14 4:00am    
You're welcome :)

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