Sunday, January 2, 2022

ASP.NET CRUD-2 with SQL Server CheckBoxList control

This post is continuation of ASP.NET CRUD-1 with SQL Server TextBox control. Refer that post for the ASP.NET WebForm project creation and establishing connection with the SQL Server database.

NOTE:-

CODE:


using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI;

namespace CRUD_ASP
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Response.Write("<h2>iNSERT DATA:</h2>");
            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            string firstname = FirstNameTextBox.Text;
            string lastname = LastNameTextBox.Text;
            string email = EmailTextBox.Text;
            string languages = "";

            foreach (System.Web.UI.WebControls.ListItem item in LanguagesCheckBoxList.Items)
            {
                if (item.Selected)
                {
                    languages += item.ToString();
                }
            }
            bool IsVB = (languages.IndexOf("VB.NET") != -1) ? true : false;
            bool IsJava = (languages.IndexOf("Java") != -1) ? true : false;
            bool IsCSharp = (languages.IndexOf("C#") != -1) ? true : false;
            string constring = ConfigurationManager.ConnectionStrings["csx"].ConnectionString;
            try
            {
                using (SqlConnection conn = new SqlConnection(constring))
                {
                    string query = "INSERT INTO tbl_Employee (FirstName,LastName,Email, IsVB, IsJava, IsCSharp) VALUES( @FirstName, @LastName, @Email,@IsVB, @IsJava, @IsCSharp)";
                    using (SqlCommand command = new SqlCommand(query, conn))
                    {
                        command.Parameters.AddWithValue("@FirstName", firstname);
                        command.Parameters.AddWithValue("@LastName", lastname);
                        command.Parameters.AddWithValue("@Email", email);
                        command.Parameters.AddWithValue("@IsVB", IsVB);
                        command.Parameters.AddWithValue("@IsJava", IsJava);
                        command.Parameters.AddWithValue("@IsCSharp", IsCSharp);
                        conn.Open();
                        command.ExecuteNonQuery();
                        string script = "window.onload = function(){ alert('Details inserted successfully.')};";
                        ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
                    }
                }
            }
            catch (Exception)
            {
                Response.Write("<h1>Error in data insertion.</h1>");
            }
        }
    }
}

© अजीत कुमार, सर्वाधिकार सुरक्षित।

इस आलेख को उद्धृत करते हुए इस लेख के लिंक का भी विवरण दें। इस आलेख को कॉपीराइट सूचना के साथ यथावत साझा करने की अनुमति है। कृपया इसे ऐसे स्थान पर साझा न करें जहाँ इसे देखने के लिए शुल्क देना पडे।

No comments:

Post a Comment

Hot Topics