输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误:
行 100: for (int j = 0; j< 6; j++)
行 101: {
行 102: Count[j] = Convert.ToInt32(my1ds.Tables[0].Rows[j][1].ToString());这行报错,为什么啊?
行 103: SolidBrush mybrush = new SolidBrush(Color.Red);
这个是程序部分
//显示柱状效果
x = 70;
for (int j = 0; j< 6; j++)
{
Count[j] = Convert.ToInt32(my1ds.Tables[0].Rows[j][1].ToString());
SolidBrush mybrush = new SolidBrush(Color.Red);
graphics.FillRectangle(mybrush, x, 366 - Count[j] * 26 / 10, 20, Count[j] *26 / 10);
x = x + 40;
}
graphics.DrawString("统计日期:", new Font("宋体", 15),Brushes.Red, new PointF(380, 60));
System.IO.MemoryStream MStream = new System.IO.MemoryStream();
image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif);//
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(MStream.ToArray());
}
数据库里的字段都是文本类型的 数据是10的话就没有问题但是是10.5就报错
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误:
行 100: for (int j = 0; j< 6; j++)
行 101: {
行 102: Count[j] = Convert.ToInt32(my1ds.Tables[0].Rows[j][1].ToString());这行报错,为什么啊?
行 103: SolidBrush mybrush = new SolidBrush(Color.Red);
这个是程序部分
//显示柱状效果
x = 70;
for (int j = 0; j< 6; j++)
{
Count[j] = Convert.ToInt32(my1ds.Tables[0].Rows[j][1].ToString());
SolidBrush mybrush = new SolidBrush(Color.Red);
graphics.FillRectangle(mybrush, x, 366 - Count[j] * 26 / 10, 20, Count[j] *26 / 10);
x = x + 40;
}
graphics.DrawString("统计日期:", new Font("宋体", 15),Brushes.Red, new PointF(380, 60));
System.IO.MemoryStream MStream = new System.IO.MemoryStream();
image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif);//
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(MStream.ToArray());
}
数据库里的字段都是文本类型的 数据是10的话就没有问题但是是10.5就报错
说明你这个不是数字字符串int a;
int.TryParse(my1ds.Tables[0].Rows[j][1].ToString(), out a);
可以使用 int.TryParse
单步调试值
Count[j] = Convert.ToInt32(my1ds.Tables[0].Rows[j][1].ToString());
Count[j] = Convert.ToInt32(my1ds.Tables[0].Rows[j][1].ToString());
的表里总4个字段
都是文本型的 为了方便显示0.5 1.5这样的数字其他类型的都无法存储,我是新手哈大家见谅。
前台显示就报数据类型错,要是字段数据是2啊3啊
这样的整数就没有任何错误。想问下大家是不是这里的写法是错的
如Convert.ToInt32不应该转这样的或数据库的字段类型要改,我要求是数据库里能写入0.5,1.5这样的数据,前台也能读出这样的数据而不是四舍五入。
调试时候看这个值是什么就知道原因所在了
取消还是报
输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误:
行 102: {
行 103:
行 104: Count[j] = Convert.ToInt32(my1ds.Tables[0].Rows[j][1]);
行 105: SolidBrush mybrush = new SolidBrush(Color.Red);
行 106: graphics.FillRectangle(mybrush, x, 366 - Count[j] * 26 / 10, 20, Count[j] *26 / 10);
我的数据是10.5多一个小数就错,是10的话就正确 请问我要实现小数形式的输出如何操作
改了之后报这样的错
编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误消息: CS0266: 无法将类型“decimal”隐式转换为“int”。存在一个显式转换(是否缺少强制转换?)源错误: 行 43: myda.Fill(myds);
行 44:
行 45: sum = Convert.ToDecimal(myds.Tables[0].Rows[0][0].ToString());
Count[]你这个数组当然要改成decimal类型了。。
一下是字段说明回笼资金完成比例 销售额完成比例 回笼资金 销售额
100 100 20 10
80 6.5 20 10
70 20 20 10
60 30 20 10
50 40 20 10
40 50 20 10其中销售额比例数据如果是6的话就完全可以不报任何错,但是要是输入6.5的话就报
输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误:
行 100: for (int j = 0; j< 6; j++)
行 101: {
行 102: Count[j] = Convert.ToInt32(my1ds.Tables[0].Rows[j][1].ToString());这行报错,为什么啊?
行 103: SolidBrush mybrush = new SolidBrush(Color.Red);
程序如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data.OleDb;
public partial class Info : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CreateImage();
}
private void CreateImage()
{
int height = 400, width = 600;
Bitmap image = new Bitmap(width, height);
//创建Graphics类对象
Graphics graphics = Graphics.FromImage(image);
try
{
int sum = 0;
string P_str_sum = "select SUM(销售额) from tb_stock";
OleDbConnection sqlcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"eg.mdb"));
OleDbDataAdapter myda = new OleDbDataAdapter(P_str_sum, sqlcon);
DataSet myds = new DataSet();
myda.Fill(myds);
sum = Convert.ToInt32(myds.Tables[0].Rows[0][0].ToString());
//清空图片背景色
graphics.Clear(Color.White);
Font font = new Font("Arial", 9, FontStyle.Regular); Font font1 = new Font("宋体", 20, FontStyle.Regular);//
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
graphics.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
graphics.DrawString("冲刺百亿目标 倒计时", font1, brush1, new PointF(150, 30));
graphics.DrawString("(销售额总计:" + sum + "亿)", new Font("楷体_gb2312", 10), Brushes.Red, new PointF(180, 60));
//画图片的边框线
graphics.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
Pen mypen = new Pen(brush, 1);
//绘制横向线条
int x = 100;
for (int i = 0; i < 11; i++)
{
graphics.DrawLine(mypen, x, 80, x, 366);
x = x + 40;
}
Pen mypen1 = new Pen(Color.Blue, 2);
graphics.DrawLine(mypen1, x - 480, 80, x - 480, 366);
//绘制纵向线条
int y = 106;
for (int i = 0; i < 10; i++)
{
graphics.DrawLine(mypen, 60, y, 540, y);
y = y + 26;
}
graphics.DrawLine(mypen1, 60, y, 540, y);
//x轴
String[] n = { " 合计", " 南京", " 常州", " 无锡", " 苏州", " 上海"};
x = 60;
for (int i = 0; i < 6; i++)
{
graphics.DrawString(n[i].ToString(), font, Brushes.Red, x, 374); //设置文字内容及输出位置
x = x + 40;
}
//y轴
String[] m = {"100%", " 90%", " 80%", " 70%", " 60%", " 50%", " 40%", " 30%",
" 20%", " 10%", " 0%"};
y = 98;
for (int i = 0; i < 11; i++)
{
graphics.DrawString(m[i].ToString(), font, Brushes.Red, 25,y); //设置文字内容及输出位置
y = y + 26;
}
int[] Count = new int[6];
string P_str_yearID = "select 回笼资金完成比例,销售额完成比例 from tb_stock";
OleDbDataAdapter my1da = new OleDbDataAdapter(P_str_yearID,sqlcon);
DataSet my1ds= new DataSet();
my1da.Fill(my1ds);
//显示柱状效果
x = 70;
for (int j = 0; j< 6; j++)
{
Count[j] = Convert.ToInt32(my1ds.Tables[0].Rows[j][1].ToString());
SolidBrush mybrush = new SolidBrush(Color.Red);
graphics.FillRectangle(mybrush, x, 366 - Count[j] * 26 / 10, 20, Count[j] *26 / 10);
x = x + 40;
}
graphics.DrawString("统计日期:", new Font("宋体", 15),Brushes.Red, new PointF(380, 60));
System.IO.MemoryStream MStream = new System.IO.MemoryStream();
image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif);//
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(MStream.ToArray());
}
finally
{
graphics.Dispose();
image.Dispose();
}
}
}