I am having a problem to pass float value to database, Actually in swedish language thousand seperator is white space. So when i am going to pass the float value to database white space is also passing to database. and query fail to enter the data.
string variable Vinst is saving the float value, its appear like this [6 000,00] so i replace the , with . otherwise my store procedure give error to many paramters. But i cant not able to remove the thousand separator in the value.
Here is my Code
try {
DbHandler handler = new DbHandler();
string SQL = string.Empty,Antal=string.Empty,Pris=string.Empty,Kostnad=string.Empty,Vinst=string.Empty;
string Klockan = string.Empty, Fritext = string.Empty, moms = "25", SystemUser = this.Session["UserID"].ToString(), UtrustningID=string.Empty;
for (int i = 0; i < UtrustningMaster.Rows.Count; i++){
//Get the Nested Gridview inside the Master Gridview
GridView nestedGridView = (GridView)UtrustningMaster.Rows[i].FindControl("Utrustning");
for (int j = 0; j < nestedGridView.Rows.Count; j++){
if (((CheckBox)nestedGridView.Rows[j].FindControl("cbMarked")).Checked == true){
// Fetch all the information
UtrustningID = nestedGridView.DataKeys[j]["ID"].ToString();
if (((CheckBox)nestedGridView.Rows[j].FindControl("cb12Mark")).Checked == true)
moms = "12";
Pris = ((TextBox)nestedGridView.Rows[j].FindControl("txtUtrustningPris")).Text.Trim();
Antal = ((TextBox)nestedGridView.Rows[j].FindControl("txtUAntal")).Text.Trim();
Kostnad = ((TextBox)nestedGridView.Rows[j].FindControl("txtUtrustningKostnad")).Text.Trim();
Vinst = ((Label)nestedGridView.Rows[j].FindControl("lbUtrustningVinst")).Text.Trim().Replace(',','.');
Klockan = ((TextBox)nestedGridView.Rows[j].FindControl("txtUtrustningKlokan")).Text.Trim();
Fritext = ((TextBox)nestedGridView.Rows[j].FindControl("txtUtrustningFritext")).Text.Trim();
// Create Boked Utrustning In KontorBokedUtrustning Table
SQL = "SP_NewBookedUtrustning ";
SQL += ""+UtrustningID+","+BookingID+",'"+BookedDate+"',"+Antal+","+Pris+","+Kostnad+","+Vinst+", ";
SQL += "'"+Klockan+"','"+Fritext+"',"+moms+","+SystemUser+"";
handler.dbQuery(SQL);
}
}
}
return "OK";
}
catch (Exception exc) {
ShowMessage("Error Message", exc.Message);
return "Error";
}
N.B.: just because at conversion is successful does not mean that the converted result is valid: what if someone enters
6 000 000 000,00 or -0.06? you need to range check the result to see whether it is reasonalbe
before you update your database.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
shahid.majee...
Member
620 Points
544 Posts
How to pass float value to sql server, Float has white space as thousand seperator
Dec 02, 2012 05:52 PM|LINK
Hi,
I am having a problem to pass float value to database, Actually in swedish language thousand seperator is white space. So when i am going to pass the float value to database white space is also passing to database. and query fail to enter the data.
string variable Vinst is saving the float value, its appear like this [6 000,00] so i replace the , with . otherwise my store procedure give error to many paramters. But i cant not able to remove the thousand separator in the value.
Here is my Code
try { DbHandler handler = new DbHandler(); string SQL = string.Empty,Antal=string.Empty,Pris=string.Empty,Kostnad=string.Empty,Vinst=string.Empty; string Klockan = string.Empty, Fritext = string.Empty, moms = "25", SystemUser = this.Session["UserID"].ToString(), UtrustningID=string.Empty; for (int i = 0; i < UtrustningMaster.Rows.Count; i++){ //Get the Nested Gridview inside the Master Gridview GridView nestedGridView = (GridView)UtrustningMaster.Rows[i].FindControl("Utrustning"); for (int j = 0; j < nestedGridView.Rows.Count; j++){ if (((CheckBox)nestedGridView.Rows[j].FindControl("cbMarked")).Checked == true){ // Fetch all the information UtrustningID = nestedGridView.DataKeys[j]["ID"].ToString(); if (((CheckBox)nestedGridView.Rows[j].FindControl("cb12Mark")).Checked == true) moms = "12"; Pris = ((TextBox)nestedGridView.Rows[j].FindControl("txtUtrustningPris")).Text.Trim(); Antal = ((TextBox)nestedGridView.Rows[j].FindControl("txtUAntal")).Text.Trim(); Kostnad = ((TextBox)nestedGridView.Rows[j].FindControl("txtUtrustningKostnad")).Text.Trim(); Vinst = ((Label)nestedGridView.Rows[j].FindControl("lbUtrustningVinst")).Text.Trim().Replace(',','.'); Klockan = ((TextBox)nestedGridView.Rows[j].FindControl("txtUtrustningKlokan")).Text.Trim(); Fritext = ((TextBox)nestedGridView.Rows[j].FindControl("txtUtrustningFritext")).Text.Trim(); // Create Boked Utrustning In KontorBokedUtrustning Table SQL = "SP_NewBookedUtrustning "; SQL += ""+UtrustningID+","+BookingID+",'"+BookedDate+"',"+Antal+","+Pris+","+Kostnad+","+Vinst+", "; SQL += "'"+Klockan+"','"+Fritext+"',"+moms+","+SystemUser+""; handler.dbQuery(SQL); } } } return "OK"; } catch (Exception exc) { ShowMessage("Error Message", exc.Message); return "Error"; }Shahid Majeed
Email: shahid.majeed@gmail.com
shahid.majee...
Member
620 Points
544 Posts
Re: How to pass float value to sql server, Float has white space as thousand seperator
Dec 02, 2012 06:10 PM|LINK
I solve my problem by convert it to decimal like this. May be someone have good suggestion then let me know thanks
Convert.ToDecimal(((Label)nestedGridView.Rows[j].FindControl("lbUtrustningVinst")).Text.Trim().Replace(" ", "")).ToString().Replace(",", ".").ToString()Shahid Majeed
Email: shahid.majeed@gmail.com
gerrylowry
All-Star
20513 Points
5712 Posts
Re: How to pass float value to sql server, Float has white space as thousand seperator
Dec 02, 2012 06:45 PM|LINK
@ shahid.majeed TIMTOWTDI =. there is more than one way to do it
your solution works for you ... however, it;s really important to test your program with many different values.
also, i strongly recommend that you use Decimal.TryParse (http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx) because "A return value indicates whether the conversion succeeded or failed".
N.B.: just because at conversion is successful does not mean that the converted result is valid: what if someone enters 6 000 000 000,00 or -0.06? you need to range check the result to see whether it is reasonalbe before you update your database.
g.
P.S.: if you are satified with your answer at http://forums.asp.net/post/5227447.aspx, you should mark it as "the answer".