asp.net - Error converting data type nvarchar to float -
here 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 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 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 eventstable 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 latest values..please try again."); response.write(err); } reader.close(); } } }
after try test page, getting following error:
system.data.sqlclient.sqlexception: [b]error converting data type nvarchar float. [/b] @ system.data.sqlclient.sqlconnection.onerror(sqlexception exception, boolean breakconnection) @ system.data.sqlclient.sqlinternalconnection.onerror(sqlexception exception, boolean breakconnection) @ 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() @ system.data.sqlclient.sqldatareader.get_metadata() @ system.data.sqlclient.sqlcommand.finishexecutereader(sqldatareader ds, runbehavior runbehavior, string resetoptionsstring) @ system.data.sqlclient.sqlcommand.runexecutereadertds(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, boolean async) @ system.data.sqlclient.sqlcommand.runexecutereader(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, string method, dbasyncresult result) @ system.data.sqlclient.sqlcommand.runexecutereader(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, string method) @ system.data.sqlclient.sqlcommand.executereader(commandbehavior behavior, string method) @ system.data.sqlclient.sqlcommand.executedbdatareader(commandbehavior behavior) @ system.data.common.dbcommand.executereader() @ portfolio.button11_click(object sender, eventargs e) in e:\study\last sem\project\backup\finance1\portfolio.aspx.cs:line 1187
any idea guys..
i tried debug using watch , every variable getting value shd get..so dont cause of problem :( pl me out
the executereader method used getting query (select) results datareader object. see trying perform insert. suggest use sqlcommand.executenonquery instead , see if works.
more replace
dbdatareader reader = cmd.executereader(); if (reader.hasrows) { while (reader.read()) { } }
with
cmd.executenonquery();
also check if of sqlparameters create sqldbtype.nvarchar type maps column of type float in database. use appropriate types sqlparameters.
Comments
Post a Comment