Click here to Skip to main content
15,892,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m just writing insert value to database in C#
its give me error that are describes below
VB
1)Error      ; expected   line 44 
2){ expected line:42
3)} expected line:37
4)Invalid expression term '}' line:44
5)Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement        line:43		

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        { 
            string name=textBox1.Text;
            int age=Convert.ToInt32(textBox2.Text);
            int salary=Convert.ToInt32(textBox3.Text);
                 try
				{
					string connectionstring = @"Data Source=THEONE\PARTH;Initial Catalog=testdatabase;Integrated Security=True;";
					SqlConnection conn = new SqlConnection(connectionstring);
					conn.Open();
					SqlCommand cmd=new SqlCommand("INSERT INTO persondetail VALUES (@name,@age,@salary)",conn);
					{
						cmd.Parameters.AddWithValue("@name",name);
						cmd.Parameters.AddWithValue("@age",age);
						cmd.Parameters.AddWithValue("@salary",salary);
						cmd.ExecuteNonQuery();
						conn.Close();
					}
        
					catch(SqlException ex)
					{ 
					MessageBox.Show(ex.Message);
					}
				}
		}
    }
}
Posted
Comments
ZurdoDev 3-Sep-13 8:38am    
Syntax errors.
Thanks7872 3-Sep-13 8:39am    
So we are suppose to find those line-37,42,43,44? or what?
Maarten Kools 3-Sep-13 8:43am    
Looks can be deceiving. Properly balance your curly brackets, and you'll see what your problem is.
johannesnestler 3-Sep-13 9:05am    
does not look perfect and isn't (-suprise). I don't mind "dumb" questions, but in this case you wont go far in .NET programming if you don't learn the most basic error messages of your IDE (when I think about the past IDEs I worked with, VS gives kind of "perfect" error-messages). In your case it's more than obviouse thats a syntax error. To avoid these try to write the code in the correct order, use snippets, and WONDER why automatic formatting just stopped while writing...

1 solution

You're missing a "}" immediately before the catch(SqlException ex) to terminate the try block.

BTW: why do you have a block round the code immediately below
C#
SqlCommand cmd=new SqlCommand("INSERT INTO persondetail VALUES (@name,@age,@salary)",conn);
Did you forget the using block start?

[edit]
Try this:
C#
private void button1_Click(object sender, EventArgs e)
    {
    string name = textBox1.Text;
    int age = Convert.ToInt32(textBox2.Text);
    int salary = Convert.ToInt32(textBox3.Text);
    try
        {
        string connectionstring = @"Data Source=THEONE\PARTH;Initial Catalog=testdatabase;Integrated Security=True;";
        using (SqlConnection conn = new SqlConnection(connectionstring))
            {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("INSERT INTO persondetail VALUES (@name,@age,@salary)", conn))
                {
                cmd.Parameters.AddWithValue("@name", name);
                cmd.Parameters.AddWithValue("@age", age);
                cmd.Parameters.AddWithValue("@salary", salary);
                cmd.ExecuteNonQuery();
                conn.Close();
                }
            }
        }
    catch (SqlException ex)
        {
        MessageBox.Show(ex.Message);
        }
    }

[/edit]
 
Share this answer
 
v2

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