简体   繁体   中英

jquery UI autocomplete with asp.net c#

I am trying to do Autocomplete by taking values from database using jquery and c#.

This is my html form

<form action="Default.aspx"  method="post">
    <fieldset>
        <p class="ui-widget">
            <label for="state">State (abbreviation in separate field):
            </label>
            <input type="text" id="state"  name="state" /> 
            <input readonly="readonly" type="text" id="abbrev" name="abbrev" maxlength="2" size="2"/>
        </p>
        <input type="hidden" id="state_id" name="state_id" />
        <p class="ui-widget">
            <label for="state_abbrev">State (replaced with abbreviation):
            </label>
            <input type="text" id="state_abbrev" name="state_abbrev" />
        </p>
        <p>
            <input type="submit" name="submit" value="Submit" />
        </p>
    </fieldset>
</form>

and my JQuery file is this

$(function () {
    $('#abbrev').val("");
    $("#state").autocomplete({
        source: "states.aspx",
        minLength: 2,
        select: function (event, ui) {
            $('#state_id').val(ui.item.id);
            $('#abbrev').val(ui.item.abbrev);
        }
    });
    $("#state_abbrev").autocomplete({
        source: "states_abbrev.aspx",
        minLength: 2
    });
});

.cs file is this

JavaScriptSerializer serializer;
public class State {
    public int id;
    public string value;
    public string abbrev;
}  
protected void Page_Load(object sender, EventArgs e) {
    serializer = new JavaScriptSerializer();
    Response.Write(JSONData(Request.QueryString["Term"]));
}   
private string JSONData(string term) {
    ArrayList stateArray = new ArrayList();
    int index = 0;
    SqlConnection objConn = new SqlConnection("YOUR-CONNECTION-STRING-HERE");
    DataSet myds = new DataSet("States");
    objConn.Open();
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT id, state, abbrev FROM states WHERE state like \'%\' + @ac_term + \'%\'", objConn);
    adapter.SelectCommand.Parameters.Add("@ac_term", SqlDbType.VarChar);
    adapter.SelectCommand.Parameters["@ac_term"].Value = term;
    adapter.Fill(myds, "States");
    foreach (DataRow dr in myds.Tables[0].Rows) {
        State st = new State();
        st.id = dr["id"].ToString();
        st.value = dr["state"].ToString();
        st.abbrev = dr["abbrev"].ToString();
        stateArray.Add(st);
    }
    objConn.Close();
    return serializer.Serialize(stateArray);
}

but still im gettin error in this line

adapter.Fill(myds, "States");

can anyone help me out of this...

I believe the problem is in this line:

adapter.SelectCommand.Parameters["@ac_term"].Value = term;

The error is telling you that this parameter does not have a value. You either need to ensure that term equals something or alter your stored procedure to not rely on this parameter.

From your code it appears that the variable you pass to the JSONData function comes from a Querystring, you should test to ensure that this string is what you expect it to be (ie NOT null or undefined or something else you don't expect).

An easy way to do this would be:

if (string.IsNullOrEmpty(term) || term == "undefined") return;

This will cause the function to return if it encounters a null or an undefined querystring.

On first look, it seems like \\'%\\' is the problem - don't use escape characters - just: SELECT id, state, abbrev FROM states WHERE state like '%' + @ac_term + '%'

Cheers, Ivan

Try accounting for a NULL value:

SqlDataAdapter adapter = new SqlDataAdapter(@"SELECT id, state, abbrev FROM states WHERE @ac_term IS NULL OR state LIKE '%' + ISNULL(@ac_term,'') + '%'", objConn);

(using @ to eliminate the escape chars)

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