2
votes

Here is the code:

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();
                }

            }
    }

After I try to test the page, I am getting the following error:

System.Data.SqlClient.SqlException: [b]Error converting data type nvarchar to float. [/b] at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at 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 method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader() at portfolio.Button11_Click(Object sender, EventArgs e) in e:\Study\LAST SEM\Project\Backup\Finance1\portfolio.aspx.cs:line 1187

Any idea guys..

I also tried to debug using watch and every variable is getting the value which it shd get..so I dont get the cause of the problem :( pl help me out

1
Is there a reason why you're using DbConnection/DbCommand instead of SqlConnection/SqlCommand? Also, it seems more likely that your table sell_activity may have a data type that different than what you have in your INSERT statement / SQL parameters. Take a look at sell_activity and make sure that each column that needs to be of type float is a float.bitxwise

1 Answers

1
votes

The ExecuteReader method is usually used for getting the query (SELECT) results as a DataReader object. But I see that you are trying to perform an insert. I suggest to use SqlCommand.ExecuteNonQuery instead and see if it works.

More exactly replace

DbDataReader reader = cmd.ExecuteReader();

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

with

cmd.ExecuteNonQuery();

Also check if any of the SqlParameters that you create with the SqlDbType.NVarChar type maps to a column of type float in the database. Use appropriate types for the SqlParameters.