Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
THIS IS MY C# CODE

C#
private void button5_Click(object sender, EventArgs e)
        {
            string connection = @"Data Source=(local);Initial Catalog=Project;Integrated        Security=True";
            SqlConnection sc = new SqlConnection(connection);
            string query2 = "insert into dbo.temp1 (weight,height,bmi)values('" + this.WeightTxt.Text + "','" + this.heightTxt.Text+ "','"+this.ResultTxt.Text+"'";
            SqlConnection conn = new SqlConnection(connection);
            SqlCommand cmdDataBase2 = new SqlCommand(query2, conn);
            SqlDataReader myReader;
            try
            {
                conn.Open();
                myReader = cmdDataBase2.ExecuteReader();
                MessageBox.Show("Data Saved Bacchi..");
                
                while (myReader.Read())
                {

                }
                conn.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


THIS IS MY DATABASE CREATE STATEMENT

SQL
create table temp1
(
weight numeric(3),
height numeric (3),
bmi numeric(7,7)
);
Posted
Updated 24-Feb-14 19:55pm
v2

see your query it missing the closing ) for the values block.
try below and check if that works...


C#
string query2 = "insert into dbo.temp1 (weight,height,bmi)values('" + this.WeightTxt.Text + "','" + this.heightTxt.Text+ "','"+this.ResultTxt.Text+"'");
 
Share this answer
 
Comments
♥…ЯҠ…♥ 25-Feb-14 3:00am    
Good catch ;-)
Faisalabadians 25-Feb-14 8:29am    
yeah, he is missing some thing... also when he is storing query in string variable he must use expression {0},{1},{2} so that code should looks good
Hi
When you are declaring table the table, make use of numeric data type properly. It can be like below
create table temp1
(
weight numeric(3,2),
height numeric (3,2),
bmi numeric(7,7)
);


Generally weight and height have decimal values. So when you are inserting data into the table that you have declared (i'm refering to the table syntax you have writen), we will get the below error
Arithmetic overflow error converting numeric to data type numeric.

I think the problem is with table declaration
 
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