Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
here with attched my coding it shows following error can anyone correct the error.

Error:

SQL
Error converting data type varchar to float.


Error Codding:


price_range = BulletedList3.Items[e.Index].Value;
       if (con.State == ConnectionState.Closed)
       {
           con.Open();
       }
       SqlCommand cmd = new SqlCommand("select pro_img,pro_name,price from product where price= '" + price_range + "'and status='A' and type='retail' ", con);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       da.Fill(ds);
       DataList1.DataSource = ds;
       DataList1.DataBind();
       con.Close();
Posted
Updated 18-Nov-11 18:56pm
v2
Comments
srinivas vadepally 19-Nov-11 4:04am    
Narmatha,
Datatypes of attribute from front end and back end(Database) should be equal.
You are inserting price in textbox, treated as string.
So the exception has come.

if your price column is float in Database

use it
C#
SqlCommand cmd = new SqlCommand("select pro_img,pro_name,price from product where price= " + price_range.ToString() + " and status='A' and type='retail' ", con);
 
Share this answer
 
v3
Comments
Mehdi Gholam 19-Nov-11 1:39am    
Clarification : numbers should not be in quotes in SQL queries.
Mehdi Gholam 19-Nov-11 1:40am    
5'ed
Use float.tryparse while conversion.. I think you know how to use it , if not then follow this link


http://msdn.microsoft.com/en-us/library/994c0zb1.aspx[^]
 
Share this answer
 
Comments
Member 11837260 8-Mar-16 11:19am    
if anybody can help me with this, i have the same problem: "Error converting data type to float" ?

// This is my Button

protected void Button1_Click(object sender, EventArgs e)
{
txtDate3.Text = txtDatepicker.Text.Substring(0, 2);
txtDate2.Text = txtDatepicker.Text.Substring(2, 4);
txtDate1.Text = txtDatepicker.Text.Substring(6, 4);
txtDateTampon_1.Text = txtDate1.Text + txtDate2.Text + txtDate3.Text + " 00:00:00";

txtDate4.Text = txtDatepicker0.Text.Substring(0, 2);
txtDate5.Text = txtDatepicker0.Text.Substring(2, 4);
txtDate6.Text = txtDatepicker0.Text.Substring(6, 4);
txtDateTampon_2.Text = txtDate6.Text + txtDate5.Text + txtDate4.Text + " 00:00:00";


DateTime dt1 = DateTime.Parse(txtDateTampon_1.Text);
DateTime dt3 = DateTime.Parse(txtDateTampon_2.Text);
DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());


if (txtDatepicker.Text == "" || txtDatepicker0.Text == "")
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Vous devez remplir tous les champs!')", true);
}

else if (dt1 > dt3)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Les dates saisies ne conviennent pas')", true);
}

else if ((dt1 > dt2) || (dt3 > dt2))
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Date saisie postérieure à la date du jour')", true);
}


else if ((txtDateTampon_1.Text == "dd/mm/yyyy") || (txtDatepicker0.Text == "dd/mm/yyyy"))
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('il faut entrer une date valide!')", true);
}

else
{


SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["servicegenerauxConnectionString"].ToString();
con.Open();


strSql = " select NoCommande, NomEntreprise, NoFacture, EtatCommande, CoutTotal, Rabais, Frais, DateCommande, DateLivraison, DatePaiement, NumeroCompte from Commandes where DatePaiement between '" + convertQuotes(txtDateTampon_1.Text) + "' and '" + convertQuotes(txtDateTampon_2.Text) + "' order by NoCommande";


cmd = new SqlCommand(strSql, con);
SqlCommand myCommand = new SqlCommand(strSql, con);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

con.Close();

GridView1.AllowPaging = false;
GridView1.DataBind();



using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["servicegenerauxConnectionString"].ToString()))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
conn.Open();
for (int j = 0; j <= GridView1.Rows.Count - 1; j++)
{


strSql3 = "INSERT into Tbl_CommandePaye_ParPeriode Values ('" + convertQuotes(GridView1.Rows[j].Cells[0].Text.Trim().ToString()) + "', '" + convertQuotes(GridView1.Rows[j].Cells[1].Text.Trim().ToString()) + "', '" + convertQuotes(GridView1.Rows[j].Cells[2].Text.Trim().ToString()) + "', '" + convertQuotes(GridView1.Rows[j].Cells[3].Text.Trim().ToString()) + "', '" + convertQuotes(GridView1.Rows[j].Cells[4].Text.Trim().ToString()) + "', '" + convertQuotes(GridView1.Rows[j].Cells[5].Text.Trim().ToString()) + "', '" + convertQuotes(GridView1.Rows[j].Cells[6].Text.Trim().ToString()) + "', '" + convertQuotes(GridView1.Rows[j].Cells[7].Text.Trim().ToString()) + "', '" + convertQuotes(GridView1.Rows[j].Cells[8].Text.Trim().ToString()) + "', '
Member 11837260 8-Mar-16 11:22am    
data cannot insert to my table, always telle me this msg.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900