我是初学者,使用c#。我正在尝试使用c#
中的if else语句进行编码。当我运行该程序时,会出现此错误消息。它出现在double x = Convert.ToDouble("XValue");
行。我试图将字符串X值转换为double以匹配我的变量x。
static void Main(String[] args)
{
connectDB();
OpenConnection();
MySqlCommand command = c.CreateCommand();
command.CommandText = "SELECT * FROM gazecoords INNER JOIN gazeperiod ON gazecoords.gazeID = gazeperiod.gazeID INNER JOIN trialImage on gazeperiod.imageID = trialImage.imageID INNER JOIN areacoords on trialImage.imageID = areacoords.trialImageID;";
try
{
c.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["imageID"].ToString());
Console.WriteLine(reader["topLeftX"].ToString());
Console.WriteLine(reader["topRightX"].ToString());
Console.WriteLine(reader["topLeftY"].ToString());
Console.WriteLine(reader["bottomLeftY"].ToString());
Console.WriteLine(reader["XValue"].ToString());
Console.WriteLine(reader["YValue"].ToString());
Console.WriteLine(reader["in"].ToString());
while (reader.Read())
{
double x = Convert.ToDouble("XValue");
double y = Convert.ToDouble("YValue");
List <int> image = new List<int>(Convert.ToInt32("imageID"));
// int coordsID = Convert.ToInt32("coordsID");
double topLeftX = Convert.ToDouble("topLeftX");
double topRightX = Convert.ToDouble("topRightX");
double topLeftY = Convert.ToDouble("topLeftY");
double bottomLeftY = Convert.ToDouble("bottomLeftY");
int inside = Convert.ToInt32("in");
inside = 0;
foreach (int coordsID in image)
{
if (x > topLeftX && x < topRightX && y > topLeftY && y < bottomLeftY)
{
Console.WriteLine("The value inside are the area is {0}", inside + 1);
}
else
Console.WriteLine("0");
Console.ReadLine();
}
}
}
}
答案 0 :(得分:0)
应该是
double x = Convert.ToDouble(reader["XValue"]);
它试图将字符串“XValue”转换为Double,这是不可能的,但现在它将转换reader["XValue"]
的值。
答案 1 :(得分:0)
您没有将reader["XValue"]
传递给double x = Convert.ToDouble("XValue");
XValue
是string
,在转换为double
时导致错误。
将其更改为
double x = Convert.ToDouble(reader["XValue"]);
答案 2 :(得分:0)
也许, XValue 是 null :
double x = Convert.ToDouble(reader["XValue"] ?? 0);