Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I work on ASP.NET MVC auto complete project. I face issue I can't display Employee Name on input text Id LineManagerName when changing auto complete employeeId.

Employee Id represents:

@Html.EditorFor(model => model.LineManager, new { htmlAttributes = new { @class = "form-control", id = "txtLineManagerId" } })

Employee Name represent

When select employee Id from list of auto complete employee Name not display on input text LineManagerName based on changed id on txtLineManagerId

What I have tried:

public ActionResult GetAllEmployeeBasedSearchText(string searchText)
        {
            JDEUtility jde = new JDEUtility();
         
            List<object> employeeListCriteria = new List<object>();
            employeeListCriteria = jde.GetAllEmployeeBasedSearchText(searchText);
            return Json(employeeListCriteria, JsonRequestBehavior.AllowGet);

        }
       public static List<object> GetAllEmployeeBasedSearchText(string searchText)
        {
            OleDbConnection con = new OleDbConnection(connectionString);
        
            string query = "";

            query = "SELECT cast(EMP.YAAN8 as varchar(20)) as EmployeeID,EMP.YAALPH AS EmployeeName FROM CRPDTA.F060116 EMP WHERE cast(EMP.YAAN8 as varchar(20)) LIKE '%" + searchText + "%' WITH UR";


            List<object> ApplicationsDataValues = new List<object>();
            try
            {
                
                using (var command = con.CreateCommand())
                {
                    command.CommandText = query;
                    command.CommandType = CommandType.Text;


                    command.Connection.Open();

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            ApplicationsDataValues.Add(new
                            {
                                
                                EmployeeID = reader.GetFieldValue<string>(0) 

                            });
                        }
                        reader.Close();
                    }

                }
                con.Close();
                return  ApplicationsDataValues;  
            }
            catch (Exception e)
            {
                return new List<object>();
            }
        }

    $(document).ready(function () {
        $("#txtLineManagerId").autocomplete({
            source: function (request, response) {
                var searchText = $("#txtLineManagerId").val();
                console.log("search text" + searchText)
                $.ajax({
                    url: '@Url.Action("GetAllEmployeeBasedSearchText", "Resignation")',
                    data: { searchText: searchText },
                    method: "GET",
                    dataType: "json",
                    success: function (data) {
                   
                        response($.map(data, function (item) {
                            console.log("data is" + item.EmployeeID);
                            return { label: item.EmployeeID, value: item.EmployeeID };

                        }))

                    }
                });
            }
        });
    });
Posted
Updated 29-Aug-23 1:32am
v2

1 solution

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Comments
Richard Deeming 24-Aug-23 4:21am    
Good luck! We've been whacking him round the head with an industrial-scale cluebat on that issue, amongst others, for 14 years.

Ahmed is our resident Nosferatu-level help vampire. He's built his entire career on getting other people to do his work for him.
OriginalGriff 24-Aug-23 4:44am    
I really should start checking names before answering things ... :doh:
I remember this one: memory shorter than a goldfish, and no idea how to write any code whatsoever. Or too lazy to bother, whichever.
Andre Oosthuizen 24-Aug-23 5:40am    
Quote: I remember this one - my +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900