Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Cannot insert data into database

$
0
0

I'm working on registration page that user will fill the form and sumbit into database. I got the problem with database. Can anyone shared with me idea/solution. Thanks.

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

public partial class Registration : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();

    protected void Page_Load(object sender, EventArgs e)
    {

        SqlCommand cmd = new SqlCommand();
        SqlConnection con = new SqlConnection();

        con.ConnectionString = @"Data Source= (LocalDB)\MSSQLLocalDB; AttachDbFilename = 
           C:\Users\Nor  Asyraf  Mohd No\Documents\Visual Studio 2017\LuckyDraw\LuckyDraw\App_Data\lucky.mdf; 
            Integrated Security = True";


    }
    public void refresh()

    {

        txtId.Text = "";

        txtName.Text = "";

        DropDownList1.Text = "";

    }



    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        SqlCommand cmd = new SqlCommand("insert into EMPLOYEES(EMP_ID,EMP_NAME,EMP_DEPARTMENT) values('" + txtId.Text + "','" + txtName.Text + "','" + DropDownList1.Text + "')",con);
        cmd.CommandType = CommandType.Text;

        try

        {

            con.Open();

            cmd.ExecuteNonQuery();

            lblResult.Text = "Data inserted successfully";

            con.Close();

            refresh();

        }

        catch (Exception ex)

        {

            lblResult.Text = ex.Message;

        }


    }

    protected void btnClear_Click(object sender, EventArgs e)
    {

        refresh();

        lblResult.Text = "";

    }
}


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>