简体   繁体   中英

C# - Sql Query If Where Clause is there then working if not there no result

Please solve my problem

I have following Code

 jQuery.ajax({ type: 'POST', url: "Default.aspx/populate_From", data: JSON.stringify({ term: aterm }), contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { alert(data.d); //jQuery(containerFrom).html(data.d); //forceWidth(containerFrom, jQuery(containerFrom).width()); }, error: function (x, e) { alert(e.Message); alert(jsonResponse.message); jQuery(containerFrom).text("Error, unable to retrieve list."); } }); 
 [WebMethod] public static string populate_From(string term) { string query = "SELECT * FROM route_onlinemaptable WHERE isdelete = 0 AND (route_from LIKE '" + term + "%' OR map_from LIKE '" + term + "%') ORDER BY priority DESC, state_from ASC, map_from ASC"; if (string.IsNullOrWhiteSpace(term)) { query = "SELECT * FROM route_onlinemaptable"; } SqlCommand cmd = new SqlCommand(query); return GetData(cmd).GetXml(); } public static DataSet GetData(SqlCommand cmd) { String strConnString = ConfigurationManager.ConnectionStrings["myConnStrng"].ConnectionString; using (SqlConnection con = new SqlConnection(strConnString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataSet ds = new DataSet()) { sda.Fill(ds); return ds; } } } } 

Query = SELECT * FROM route_onlinemaptable --> Not Showing Any Result
If Query = SELECT * FROM route_onlinemaptable where route_from = 'SOME VALUE' --> Showing Result

My Database is Attached to Application & Table Contains nearly 500 - 600 Rows.

Please kindly suggest any suggestions.

please add one condition

check if term is null or empty then dont append where condition

string query = "";
    if(term = "")
    query = "SELECT * FROM route_onlinemaptable WHERE isdelete = 0"
    else
    query = "SELECT * FROM route_onlinemaptable WHERE isdelete = 0 AND (route_from LIKE '" + term + "%' OR map_from LIKE '" + term + "%') ORDER BY priority DESC, state_from ASC, map_from ASC";

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