简体   繁体   中英

How to create a search that will not accept a certain character

I am trying to create a search that when the user searches for something with a semi colon will return with an error. This is what I have so far any improvements to the code would be great as well!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Assignment1
{
public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void SUBMIT_Click(object sender, EventArgs e)
    { 


        {
            string sConnection = "Data Source=labsql;Integrated Security=True;database=master";
            SqlConnection dbConn;
            dbConn = new SqlConnection(sConnection);
            dbConn.Open();

            string sql = "SELECT * FROM you_found_the_easter_egg WHERE Message LIKE '%" + DataInputBox.Text + "%' OR IsThisFunnyToJeff LIKE '%" + DataInputBox.Text + "%' OR George LIKE '%" + DataInputBox.Text + "%';";
            SqlCommand dbCmd = new SqlCommand(sql);
            dbCmd.Connection = dbConn;

            SqlDataReader reader = dbCmd.ExecuteReader();

            while (reader.Read())

            reader.Close();
            dbCmd.ExecuteNonQuery();
            dbConn.Close();
        }

        }

    }
}

Also, in terms of presenting the results what should I use for that? Would a list box be the easiest way to present the results?

Try using the valid chars attribute to specify in the textbox the characters you want to accept

https://docs.microsoft.com/en-us/aspnet/web-forms/overview/ajax-control-toolkit/filteredtextbox/allowing-only-certain-characters-in-a-text-box-cs

<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
 TargetControlID="TextBox1" ValidChars="1234567890"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM