I've been getting errors which are terrible :D
public static List<tableName> GetUserStatus(IUser user)
{
var result = new List<tableName>();
var database = new Database("dibot");
var str = string.Format("SELECT * FROM dibott WHERE userid = '{0}'", user.Id);
var tableName = database.FireCommand(str);
while (tableName.Read())
{
var userId = (string)tableName["userid"];
var userName = (string)tableName["username"];
var currentTokens = (int)tableName["tokens"]; // Here
result.Add(new tableName
{
userid = userId,
username = userName,
tokens = currentTokens
});
}
database.CloseConnection();
return result;
}
At this command
[Command("Status")]
[Alias("status", "s")]
public async Task Status([Remainder] IUser user = null)
{
var embed = new EmbedBuilder()
{
Color = new Color(0, 0, 255)
};
if (user == null)
{
user = Context.User;
}
var result = Database.CheckExistingUser(user); //Here
if (result.Count() <= 0)
{
Database.EnterUser(user);
}
var tableName = Database.GetUserStatus(user);
I've tried everything please help. I tried much things. Nothing helped Asking for help because im out of ideas and in internet there arent any fixes I've put // To see where are the errors. I hope i get helped soon! :)
So the problematic line is this:
var currentTokens = (int)tableName["tokens"];
InvalidCastException
means that the type contained in tableName["tokens"]
is not int
. To get at the actual type you can for example remove the cast, put a breakpoint on that line and check with the debugger.
From your comment we know that the database type is a varchar, which means you need to use string in .NET:
var currentTokens = (string)tableName["tokens"];
since you are casting tokens into int
im assuming its a numeric column.
If you have TINYINT
then use byte
, if its unsigned then use ubyte
if you have SMALLINT
then use short
, or ushort
if its unsigned
if you have MEDIUMINT/INT
then use int
or uint
for unsigned
if you have BIGINT
then use long
or ulong
for unsigned
best way to find out is to do a tableName["tokens"].GetType()
.
If you don't cast it to the right type you will get that InvalidCast
exception
Edit:
Based on your comment its a varchar
column which means its a string. if you want to make it an int then you need to do
int.Parse((string) tableName["tokens"]);
or
long.Parse((string) tableName["tokens"]);
if its a 64 bit int
But it is really a bad design to store numeric in string format and later on parse it again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.