0

I'm trying to pull data from a bunch of text boxes and then insert them into a database and all is well except for two, one is that if they are null they error but that's not the concern right now.

When I have a textbox that is supposed to take a number it says it cannot convert varchar to float. The inserts work if I do them directly in SQL Server though (using SQL Server 2008) so I know that it's something in the code.

My textbox is just a simple:

<asp:TextBox id="CID" runat="server"></asp:TextBox>

And the code I have for pulling the information from the textbox is:

float custID = float.Parse(CID.Text);

I'm new to C# so if you could help me simply put that would be better but any help works. thx

Edit: this is the line the error I get:

Line 63: 
Line 64:         sqlConnection1.Open();
Line 65:         cmd.ExecuteNonQuery();
Line 66:         sqlConnection1.Close();
Line 67:     }

Code requested:

cmd.CommandText = "INSERT INTO Customer VALUES('@custID', @mastName, @custName,      @addrLn1, @addrLn2, @cCity, @cState, @cZip, @cZip4, @cCountry, @cusSince, @renewalDate, @cNotes, @salesRep, @conMem, @ilsProd, @numbSites, @guardItEsc)";

    cmd.CommandType = CommandType.Text;
    cmd.Connection = sqlConnection1;

Parsed w/ parameter:

float custID = float.Parse(CID.Text);
    cmd.Parameters.AddWithValue("@custId", custID);
9
  • 1
    Could you tell us the content of your CID.Text ? Commented Jul 3, 2014 at 16:32
  • 2
    You'll also need to show us the code that builds cmd and executes the query (since that's where the actual error is). Commented Jul 3, 2014 at 16:32
  • Could you add the code that writes to your database? Commented Jul 3, 2014 at 16:32
  • We meant where the query is formulated. Definition of cmd Commented Jul 3, 2014 at 16:33
  • I added in the query, the connection works because it works in other parts of the webpage where I'm pulling information from the database instead of inserting Commented Jul 3, 2014 at 16:35

3 Answers 3

4

@custID has single quotes around it, so it is being treated as a varchar (literally attempting to insert the string "@custID"). Just remove the quotes.

cmd.CommandText = "INSERT INTO Customer VALUES(@custID, @mastName, @custName,      @addrLn1, @addrLn2, @cCity, @cState, @cZip, @cZip4, @cCountry, @cusSince, @renewalDate, @cNotes, @salesRep, @conMem, @ilsProd, @numbSites, @guardItEsc)";
Sign up to request clarification or add additional context in comments.

5 Comments

Yes this works for int but it doesn't help for float :(
Are you getting a different error, or the same one?
Also, I'm curious why custID (which I assume is "customerID") is a float instead of an integer?
there is no error now it just defaults to the integer without the decimal places.. It's because there is one main customer e.g 123 and that main customer can have customers under it, e.g 123.01, 123.02. Sort of like the meth dealers in Breaking Bad :)
(I don't know if this actually tags you) I noticed in my database that the float is now working. Would you perhaps know why when I put in xyz.05 it goes in as xyz.0498...so on, and how I can fix it (or if I can)
1

check for empty CID.Text and/or formatting issues, eg: 15,4 (error) -> 15.4 (ok!)

edit1: it looks like you forgot to map the parameter as in myCommand.Parameter.AddWithValue("@value",parsedValue);

4 Comments

Can you tell us your database schema also if possible paste sql query in question
This wasn't the problem, the numbers are going in as decimal point not comma
@pbrianq, maybe the numbers are going in as decimal when you enter them, but I might enter 'fred' or 'barney' You have to validate all user input before you process it further.
This is the line I have for your suggestion, it was in already but I'm pretty sure this is what you meant: editing this into OP, I didn't know you can't do code in comments
1

'@custID' in the query should be @custID.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.