将数据类型nvarchar转换为float时出错

时间:2010-11-30 21:43:08

标签: asp.net

以下是代码:

protected void Button11_Click(object sender, EventArgs e)
    {
            String usr = Membership.GetUser(false).ToString();
            int x, count = 0, avail_shares,num;
            float t_c, c_p, c_t,ret,com,t_r,pr;
            String comp = this.DropDownList4.SelectedValue.ToString();

            avail_shares = Convert.ToInt32(this.TextBox10.Text);
            pr = float.Parse(this.TextBox15.Text);
            t_c = float.Parse(this.TextBox14.Text);
            c_p = float.Parse(this.TextBox12.Text);
            c_t = float.Parse(this.TextBox131.Text);
            ret = float.Parse(this.TextBox132.Text);
            com = float.Parse(this.TextBox133.Text);
            t_r = float.Parse(this.TextBox13.Text);
            num = Convert.ToInt32(this.TextBox11.Text);

            if (num > avail_shares)
            {
                this.Label23.Text = "You cannot sell more number of shares than what you actuall have"; 
            }
            else
            {
                DbProviderFactory dbproviderfactory = DbProviderFactories.GetFactory("System.Data.SqlClient");
                using (DbConnection conn = dbproviderfactory.CreateConnection())
                {
                    string s = ConfigurationManager.ConnectionStrings["Project_FinanceConnectionString"].ConnectionString;

                    conn.ConnectionString = s;

                    conn.Open();


                    DbCommand cmd = conn.CreateCommand();
                    //DbCommand cmd1 = conn.CreateCommand();

                    cmd.CommandText = "INSERT INTO sell_activity(username,company,avail_units,old_price,total_old_price,new_price,total_new_price,number,ret,commission,total_return,timestamp)VALUES(@U,@C,@A_U,@O_P,@T_O_P,@N_P,@T_N_P,@N,@R,@C,@T_R,@T)";

                    //cmd1.CommandText = "SELECT EventID from EventsTable where EventName = ename";                    


                    SqlParameter U = new SqlParameter("@U", SqlDbType.NVarChar, 256);
                    SqlParameter C = new SqlParameter("@C", SqlDbType.NVarChar, 50);
                    SqlParameter A_U = new SqlParameter("@A_U", SqlDbType.Int);
                    SqlParameter O_P = new SqlParameter("@O_P", SqlDbType.Float);
                    SqlParameter T_O_P = new SqlParameter("@T_O_P", SqlDbType.Float);
                    SqlParameter N_P = new SqlParameter("@N_P", SqlDbType.Float);
                    SqlParameter T_N_P = new SqlParameter("@T_N_P", SqlDbType.Float);
                    SqlParameter N = new SqlParameter("@N", SqlDbType.Int);
                    SqlParameter R = new SqlParameter("@R", SqlDbType.Float);
                    SqlParameter Commi = new SqlParameter("@Commi", SqlDbType.Float);
                    SqlParameter T_R = new SqlParameter("@T_R", SqlDbType.Float);
                    SqlParameter T = new SqlParameter("@T", SqlDbType.DateTime);


                    U.Value = usr;
                    C.Value = comp;
                    A_U.Value = avail_shares;
                    O_P = pr;
                    T_O_P = t_c;
                    N_P=c_p;
                    T_N_P = c_t;
                    N = num;
                    R = ret;
                    Commi = com;
                    T_R = t_r;
                    T = DateTime.Now;

                    cmd.Parameters.Add(@U);
                    cmd.Parameters.Add(@C);
                    cmd.Parameters.Add(@A_U);
                    cmd.Parameters.Add(@O_P);
                    cmd.Parameters.Add(@T_O_P);
                    cmd.Parameters.Add(@N_P);
                    cmd.Parameters.Add(@T_N_P);
                    cmd.Parameters.Add(@N);
                    cmd.Parameters.Add(@R);
                    cmd.Parameters.Add(@Commi);
                    cmd.Parameters.Add(@T_R);
                    cmd.Parameters.Add(@T);

                    [b]DbDataReader reader = cmd.ExecuteReader();[/b]
                    try
                    {
                        if (reader.HasRows)
                        {

                            while (reader.Read())
                            {

                            }
                        }
                    }
                    catch (Exception err)
                    {
                        Response.Write("Problem in retriving the latest values..Please try again.");
                        Response.Write(err);
                    }
                    reader.Close();
                }

            }
    }

尝试测试页面后,出现以下错误:

  

System.Data.SqlClient.SqlException:[b]转换数据类型时出错   nvarchar浮动。 [/ b]在   System.Data.SqlClient.SqlConnection.OnError(SqlException异常,   Boolean breakConnection)at   System.Data.SqlClient.SqlInternalConnection.OnError(SQLEXCEPTION   exception,Boolean breakConnection)at   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject   stateObj)在System.Data.SqlClient.TdsParser.Run(RunBehavior   runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,   BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject   stateObj)在System.Data.SqlClient.SqlDataReader.ConsumeMetaData()at   System.Data.SqlClient.SqlDataReader.get_MetaData()at   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,   RunBehavior runBehavior,String resetOptionsString)at   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior   cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean   async)at   System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior   cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String   方法,DbAsyncResult结果)at   System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior   cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String   方法)at   System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior   行为,String方法)at   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior   行为)在System.Data.Common.DbCommand.ExecuteReader()at   portfolio.Button11_Click(Object sender,EventArgs e)在e:\ Study \ LAST中   SEM \ Project \ Backup \ Finance1 \ portfolio.aspx.cs:第1187行

任何想法的人..

我也尝试使用watch进行调试,每个变量都获得了它所得到的值。所以我没有得到问题的原因:( pl帮帮我

1 个答案:

答案 0 :(得分:1)

ExecuteReader方法通常用于将查询(SELECT)结果作为DataReader对象获取。但我看到你正在尝试执行插入。我建议改用SqlCommand.ExecuteNonQuery,看它是否有效。

更准确地替换

DbDataReader reader = cmd.ExecuteReader();

if (reader.HasRows)
{
    while (reader.Read())
    {
    }
}

cmd.ExecuteNonQuery();

另外,检查使用SqlDbType.NVarChar类型创建的任何SqlParame是否映射到数据库中float类型的列。为SqlParameters使用适当的类型。