Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm excuting following code

OdbcConnection con;
            OdbcCommand cmd;
            con = new OdbcConnection(@"Dsn=chaitudi;dbq=C:\project\Distributor.accdb;driverid=25;fil=MS Access;maxbuffersize=2048;pagetimeout=5;uid=admin");
            con.Open();
            cmd = new OdbcCommand("insert into Company(ID,CompanyName,StreetName,City,ZipCode,State,TelePhone) values('" +textBox12.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox10.Text + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Stroed Successfully");



Bt getting the error "Numeric value out of range"...
plz help me...
thanx in advance
Posted
Updated 1-Mar-13 5:21am
v2
Comments
Richard C Bishop 1-Mar-13 11:22am    
First off, your setting yourself up for sql injection that could destroy your database. You should really consider using parameterized queries. Second, what line is the code throwing an exception?
Sergey Alexandrovich Kryukov 1-Mar-13 11:38am    
Agree. The question is nearly the same as OP's previous one, please see. OP needs to learn how to learn lessons from questions/answers, before asking another one. :-)
—SA
Mike Meinz 1-Mar-13 11:51am    
What if one of your user enters data in a textbox that is larger than or doesn't match the datatype of the associated database column?
* Error on execution of the INSERT - You need to validate user input

What if one of your users puts SQL statements to delete your database in one of the textboxes?
* SQL Injection Attack - You need to use parameters rather than concatenated strings

What if one of your users enters data in a textbox that contains an apostrophe?
* Error on execution of the INSERT - You need to replace single apostrophe with double apostrophe in any string columns.

Avoid run-time errors by coding to prevent user data entry errors!

1 solution

Since the only numbers in there are trivial: "25" and "2048" it has to be the values you are trying to insert from your textboxes. So start by checking them against the datatypes in your database.

But don't do it that way! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Also, you should be disposing your connections and commands - they are scarse commodities, so they should not be kept longer than you need to.

Oh, and stop using the VS default names for controls: you may remember today that "textbox12" holds the user ID, but you won't in a coupe of weeks time when you need to maintain this. Call them somethign sensible: it makes your code easier to read, understand and maintain. It is also quicker to type, since intelisense can sort them out quicker for you...

C#
using (OdbcConnection con = new ODbcConnection(strConnect))
    {
    con.Open();
    using (OdbcCommand com = new OdbcCommand("INSERT INTO Company (ID,CompanyName) VALUES (@UID, @CON)", con))
        {
        com.Parameters.AddWithValue("@UID", tbUserID.Text);
        com.Parameters.AddWithValue("@CON", tbCompanyName.Text);
        com.ExecuteNonQuery();
        }
    }
 
Share this answer
 

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