Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class Datalayer
{

    DataSet ds;
    SqlDataAdapter da;
    SqlCommand cmd;
    SqlConnection con;
    
	public Datalayer()
	{
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
	}

    public void inserdata(propertylayer p)
    {
        cmd = new SqlCommand("insert into info values(@name,@address,@gender,@hobby,@city,@education", con);
        cmd.Parameters.AddWithValue(@"name", p.name);
        cmd.Parameters.AddWithValue(@"address", p.address);
        cmd.Parameters.AddWithValue(@"gender", p.gender);
        cmd.Parameters.AddWithValue(@"hobby", p.hobby);
        cmd.Parameters.AddWithValue(@"city", p.city);
        cmd.Parameters.AddWithValue(@"education", p.education);
        //cmd.Parameters.AddWithValue(@"state", p.state);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

}
Posted
Updated 1-Jul-13 6:38am
v2

You forgot a close bracket:
C#
cmd = new SqlCommand("insert into info values(@name,@address,@gender,@hobby,@city,@education", con);
Should be:
C#
cmd = new SqlCommand("insert into info values(@name,@address,@gender,@hobby,@city,@education)", con);


But it is a much, much better idea to list the fields in the INSERT statement as the code then becomes more independent of the SQL table design - your code expects the columns to be defined in a specified order - the insertion of a new field at the beginning could crash your code:
SQL
cmd = new SqlCommand("INSERT INTO info (Name, Address, Gender, Hobby, City, Education) VALUES (@name,@address,@gender,@hobby,@city,@education)", con);
 
Share this answer
 
v2
If this is the real copy of the code that you are executing...then the problem is that, you forgot to close the bracket in your "insert" statement...

- The statement should be :

cmd = new SqlCommand("insert into info values(@name,@address,@gender,@hobby,@city,@education);", con);

- Don't forget to mention the name of columns after table_name in your query, in-case you are not entering values for each column that exist in the table...

Thanks and hope this helps..
 
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