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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted May 17, 2012, 1:48 AM
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
Keyur NarkhiPosted May 17, 2012, 1:42 AM
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
V S N Raju KanumuriPosted May 17, 2012, 1:26 AM
Try this
BhuvanaPosted May 17, 2012, 12:00 AM
SenthilkumarPosted May 16, 2012, 11:53 PM
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.