Hello everyone!
i have problem in inserting data in my database from textbox, i m using c#.
the code in .aspx.cs file is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page 
{
    SqlDataAdapter da = new SqlDataAdapter();
    SqlConnection sqlcon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Qarni\My Documents\Visual Studio 2008\WebSites\database-example\App_Data\Database.mdf;Integrated Security=True;User Instance=True");


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        sqlcon.Open();
        SqlCommand sqlcmd = new SqlCommand("INSERT INTO TABLE1 (Name, Address, Email, Phone, Dob) VALUES ('"+nametbox.Text+","+addtbox.Text+","+mailtbox.Text+","+phonetbox.Text+","+dobtbox.Text+"')");
        sqlcmd.Parameters.AddWithValue("@Name", nametbox.Text);
        sqlcmd.Parameters.AddWithValue("@Address", addtbox.Text);
        sqlcmd.Parameters.AddWithValue("@Email", mailtbox.Text);
        sqlcmd.Parameters.AddWithValue("@Phone", phonetbox.Text);
        sqlcmd.Parameters.AddWithValue("@Dob", dobtbox.Text);
        lbl.Visible = true;
        sqlcon.Close();



    }
}

Recommended Answers

All 4 Replies

What error are you encoutering?

You have to change/format the INSERT statement.

sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("INSERT INTO TABLE1 (Name, Address, Email, Phone, Dob) VALUES (@Name, @Address, @Email, @Phone, @Dob)";
sqlcmd.Parameters.AddWithValue("@Name", nametbox.Text);
sqlcmd.Parameters.AddWithValue("@Address", addtbox.Text);
sqlcmd.Parameters.AddWithValue("@Email", mailtbox.Text);
sqlcmd.Parameters.AddWithValue("@Phone", phonetbox.Text);
sqlcmd.Parameters.AddWithValue("@Dob", dobtbox.Text);
lbl.Visible = true;
sqlcmd.ExecuteNonQuery();
sqlcon.Close();

You Forgot to put
sqlcmd.ExecuteNonQuery();
before
sqlcon.Close();
to save the values in the database

Hope I helped

ur insert query is also incorrect with the quotation marks

SqlCommand sqlcmd = new SqlCommand("INSERT INTO TABLE1 (Name, Address, Email, Phone, Dob) VALUES ('"+nametbox.Text+"','"+addtbox.Text+"','"+mailtbox.Text+"','"+phonetbox.Text+"','"+dobtbox.Text+"')");

if ur are not using the parameters

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.