Monday, 4 April 2016

How to check if a value already exists in my database or checking user name availability


(Note: I have table column name matno with some values like SB-1003,SB-1004.....etc )


See demo video:


Screen Shot 1:
  


Screen Shot 2:


Screen Shot 3:







Aspx Code :
(for Text box and Label and Required Field Validator):-


 </td><td>
                    &nbsp;</td><td class="style3">Material Reference No</td><td class="style5">
        <asp:TextBox ID="matnotxt" runat="server"  CssClass="textbox" Height="25px" 
            Width="200px" ontextchanged="txtmatno_TextChanged"  AutoPostBack="True" ></asp:TextBox>
    </td><td>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator15" runat="server" 
            ControlToValidate="matnotxt" ErrorMessage="*" ForeColor="#CC0000"></asp:RequiredFieldValidator>
    </td><td>&nbsp;</td></tr>
<asp:Label ID="lbler" runat="server" ForeColor="#CC0000" Text="Label"></asp:Label>


C# Code :

(For text box changed event):-


    protected void txtmatno_TextChanged(object sender, EventArgs e)

    {

        if (Page.IsPostBack == true)

        {

        string connString = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
        MySqlConnection myConnection = new MySqlConnection(connString);

        using (var cmd = new MySqlCommand("select matno from sbm where matno=@matno", myConnection))
        {
            myConnection.Open();

            cmd.Parameters.AddWithValue("@matno", matnotxt.Text);
            using (var dr = cmd.ExecuteReader())
            {
                if (dr.HasRows)
                {
                    lbler.Visible = true;
                    lbler.Text = "already exists";
                }
                else
                {
                    lbler.Visible = true;
                    lbler.Text = "doesn't exists";

                }

            }
        }

        myConnection.Close();

    }
}







No comments:

Post a Comment

How to create a simple Hello World website in ASP.NET MVC using Razor Syntax: (Note: I am using Visual Studio 2012 ) Step 1: ...