我收到错误“输入字符串的格式不正确".我不知道你是什么? [英] i m getting an error "Input string was not in a correct format.". i am not able to figure out y ?

查看:100
本文介绍了我收到错误“输入字符串的格式不正确".我不知道你是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();

    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = "server=a2ul-pc;uid=sa;pwd=rascal;database=cgrt";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "insert into yearly (pcode,fyyear,yearlyalloc,salary,ta,contigency,nrc,institcharges,others) values (@pcode,@fyyear,@yearlyalloc,@salary,@ta,@contigency,@nrc,@institcharges,@others)";
        cmd.Parameters.AddWithValue("@pcode", DropDownList1.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@fyyear", TextBox10.Text +"-"+ TextBox2.Text);
        cmd.Parameters.AddWithValue("@yearlyalloc", Convert.ToInt32(TextBox3.Text));
        cmd.Parameters.AddWithValue("@salary", Convert.ToInt32(TextBox4.Text));
        cmd.Parameters.AddWithValue("@ta", Convert.ToInt32(TextBox5.Text));
        cmd.Parameters.AddWithValue("@contigency", Convert.ToInt32(TextBox6.Text));
        cmd.Parameters.AddWithValue("@nrc", Convert.ToInt32(TextBox7.Text));
        cmd.Parameters.AddWithValue("@institcharges", Convert.ToInt32(TextBox8.Text));
        cmd.Parameters.AddWithValue("@others",Convert.ToInt32(TextBox9.Text));

        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();

        DropDownList1.SelectedIndex = 0;
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        TextBox7.Text = "";
        TextBox8.Text = "";
        TextBox9.Text = "";

        Response.Write("<script type=\"text/javascript\">alert('Done!');</script>");
    }
}




[edit]已添加代码块-OriginalGriff [/edit]




[edit]Code block added - OriginalGriff[/edit]

推荐答案

检查表中的列是否与给定的数据类型兼容.还可以查看一下string和Int32到smallint列的长度.
Check that the columns of the table are compatible with the given data types. Also take a look for the lenght of strings and Int32 to smallint columns.


通常由于将空字符串转换为int"而发生这种情况.

例如:Convert.ToInt32(TextBox.Text)); //这里"TextBox.Text"为空.

在进行Int32转换之前,请确保所有"TextBox.Text" 值都为不为空" 或使用"int.TryParse".


样本:

usually this will happen because of "empty string converstion to int".

eg: Convert.ToInt32(TextBox.Text)); //here ''TextBox.Text'' is empty.

Please make sure that all the "TextBox.Text" value is "not empty" before the Int32 conversion or use "int.TryParse".


Sample:

int test1223 = -1;
         TextBox1.Text="2";
         int.TryParse(TextBox1.Text, out test1223); // 'test1223' value is "2"

         TextBox1.Text="";
         int.TryParse(TextBox1.Text, out test1223); //'test1223' value is "0"




请参考以下链接以获取有关"int.TryParse"方法的更多详细信息.

http://msdn.microsoft.com/en-us/library/f02979c7.aspx [ ^ ]




Please refer the following link for more details about "int.TryParse" method.

http://msdn.microsoft.com/en-us/library/f02979c7.aspx[^]


这很简单:您的参数之一的Convert.ToInt32函数失败,因为TextBox值不是整数-它可能不包含任何东西,或者不包含字母字符或类似字符.

我强烈建议您使用teh int.TryParse方法将所有这些都转换为int值,作为事件处理程序的第一部分,并向您的用户报告问题,以便他们知道要解决的问题.仅当值正确时,才应尝试将其传递给SQL.

在使用参数化查询方面做得很好! :thumbsup:
It''s pretty simple: the Convert.ToInt32 function is failing for one of your parameters because the TextBox value is not an integer - it may contain nothing, or an alphabetic character or similar.

I would strongly suggest that you convert these all to int values as the first part of the event handler using teh int.TryParse method, and report problems to your user so they know what to correct. Only when the values are good should you try to pass them to SQL.

Well done BTW on using parametrised queries! :thumbsup:


这篇关于我收到错误“输入字符串的格式不正确".我不知道你是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆