sir i am having a textbox which accepts the election id.
i want to check if the election id exists in the database or not.
if it doesn't exist i want to throw an alert message.
If it exists i want to display it in the grid view.
but for every correct as well as incorrect entry it is showing the alert box.
below is my code please tell the necessary changes to be made.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
public partial class searchu : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
SqlCommand cmd1 = new SqlCommand("select ElectionId from users", con);
SqlDataReader reader = cmd1.ExecuteReader();
while (reader.Read())
{
string eid = reader["ElectionId"].ToString();
if (eid == TextBox1.Text)
{
SqlDataSource SqlDataSource1 = new SqlDataSource();
this.Page.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataSource1.SelectCommand = "SELECT * FROM users WHERE ElectionId ='" + this.TextBox1.Text.Trim() + "'";
this.GridView1.DataSource = SqlDataSource1;
this.GridView1.DataBind();
this.GridView1.Visible = true;
}
else
{
string script = "window.onload = function() { alert('This user does not exist!'); }";
ClientScript.RegisterStartupScript(this.GetType(), "alert", script, true);
}
}
}
}