Hi jordan3114,
From your description, it seems that you want to avoid inserting duplicate records.
Please refer to my suggestion below:
.aspx:
<asp:TextBox ID="txtName" runat="server" AutoPostBack="True" ontextchanged="txtName_TextChanged"></asp:TextBox>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:TextBox ID="txtCountry" runat="server" ></asp:TextBox>
<asp:Button ID="btn_add" runat="server" Text="Button" onclick="Button1_Click" />
.cs:
protected void txtName_TextChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ToString()))// use your connection string here
{
SqlCommand cmd = new SqlCommand("select * from detail where d_name=@d_name", con);
cmd.Parameters.AddWithValue("@d_name", txtName.Text);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr != null)
{
while (dr.Read())
{
Response.Write("<script>alert('Name:" + dr["d_name"].ToString() + " already exists! \\nAge:" + dr["d_age"] + "\\nCountry:" + dr["d_country"] + " ')</script>");
}
}
// clear the textbox to insert new records.
txtName.Text = "";
txtAge.Text = "";
txtCountry.Text = "";
}
}
When we type a name which already exists in table “detail”,suppose “jordan”, the alert dialog will display the age and country information of “jordan”,
then the textbox controls will be cleared up to insert another new record.
Hope this helps.
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.