DropDownList:
From the dropdownlist box we select the name if the name is already register in database means we got the message as already register how i do it.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
SenthilkumarRank #387 · 6.5K reputationPosted May 16, 2012, 7:53 PM EDT
You need to do two things in the drop down list.
1) Set the property AutoPostBack=True in the drop down list
2) Write the index change event or double click the drop downlist
In the Indexchangedevent you need to get the value of selected,
ddl1.SelectedItem.Text
ddl1.SelectedItem.Value
then write the logic to check in the database, based on that you can show the error message.
Bhuvana8 reputationPosted May 16, 2012, 8:00 PM EDT
V S N Raju KanumuriRank #8,839 · 215 reputationPosted May 16, 2012, 9:26 PM EDT
Try this
Keyur Narkhi62 reputationPosted May 16, 2012, 9:42 PM EDT
You can set your name as an unique key in the Database.then on the final save if you are trying to save same name which is already in the db then it will give you an sql exception then you can handle that and display the message to user.
hope this helps.
Thanks
Satyapriya NayakRank #47 · 31.5K reputationPosted May 16, 2012, 9:48 PM EDT
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Dropdownlist_Already_Exist._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Dropdownlist_Already_Exist
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str = null;
protected void Page_Load(object sender, EventArgs e)
{
DdlUserName.AutoPostBack = true;
SqlConnection con = new SqlConnection(connStr);
if (!IsPostBack)
{
DdlUserName.Items.Add("Choose employee");
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DdlUserName.Items.Add(reader["ename"].ToString());
}
reader.Close();
con.Close();
}
}
public void namecheck()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select count(*)from employee where ename='" + DdlUserName.Text + "'";
com = new SqlCommand(str, con);
int count = Convert.ToInt32(com.ExecuteScalar());
if (count > 0)
{
lblMessage.Text = "Already Exist";
}
else
{
lblMessage.Text = "Not Exist";
}
}
protected void DdlUserName_SelectedIndexChanged(object sender, EventArgs e)
{
namecheck();
}
}
}
Thanks
If this post helps you mark it as answer