Sunday, January 2, 2022

ASP.NET CRUD-3 with SQL Server RadioButton control

This post is continuation of ASP.NET CRUD-2 with SQL Server CheckBoxList control. Refer that post before continuing this post.

NOTE:-

CODE:


using System;
using System.Windows.Forms;
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: RadioButton control</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;

            int Gender = MaleRadioButton.Checked ? 1 : (FemaleRadioButton.Checked ? 2 : 0);
            if (Gender == 0)
            {
                string scriptValue ="alert('Please select Gender.');";
                ScriptManager.RegisterStartupScript(this, this.GetType(),"ScriptKeyName", scriptValue, true);
                return;
            }

            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, Gender) 
                    VALUES( @FirstName, @LastName, @Email,@IsVB, @IsJava, @IsCSharp,@Gender)";
                    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);
                        command.Parameters.AddWithValue("@Gender", Gender);
                        conn.Open();
                        command.ExecuteNonQuery();
                        string script = "window.onload = function(){ alert('Details inserted successfully.')};";
                        ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error during Insertion.");
            }
            return;
        }
    }
}

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

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

No comments:

Post a Comment

Hot Topics