0

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();



    }
}
5
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by poojavb
0

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();
0

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

Hope I helped

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.