Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I created AutoComplete Textbox for Gridview.
I have selected value from two columns from the database, one is number (Kontonummer) and the other is text (Navn). Now when I click on it, both number and text enter the cell and it gives error. But I need for only the number column to be inserted in the cell, which is only 4 digits when I need to click on it. Can you help me with that?

What I have tried:

C#
try
{
    
    string headerText = dgvAddKontoNr.Columns["Konto"].HeaderText;

    if (dgvAddKontoNr.CurrentCell.ColumnIndex == dgvAddKontoNr.Columns["Konto"].Index)
    {
        if (headerText.Equals("Konto"))
        {
            if (e.Control is TextBox)
            {
                tb = e.Control as TextBox;
                if (tb != null)
                {
                    tb.AutoCompleteMode = AutoCompleteMode.Suggest;
                    tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    AutoCompleteStringCollection coll = new AutoCompleteStringCollection();
                    tb.AutoCompleteCustomSource = coll;
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter("SELECT CAST(Kontonummer AS nvarchar (255)) + ' - ' + Navn AS Kontonummer FROM Kontoplan WHERE LockKonto = 'True' AND KlientId = (SELECT Id FROM Klient WHERE Navn = '" + bogf.cmbKlient.Text + "')", con);
                    DataTable dt = new DataTable();
                    da.Fill(dt);

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string konto = dt.Rows[i]["Kontonummer"].ToString();
                        //string navn = dt.Rows[i]["Navn"].ToString();
                        coll.Add(konto);
                        //coll.Add(navn);
                    }
                    con.Close();


                }
                tb.MinimumSize = new Size(345, 25);
                tb.BorderStyle = BorderStyle.Fixed3D;
            }
            
        }

        else
        {
            TextBox tb = e.Control as TextBox;

            if (tb != null)
            {
                tb.AutoCompleteMode = AutoCompleteMode.None;
            }
        }
    }    
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
Posted
Updated 22-Sep-20 4:42am
v2
Comments
BillWoodruff 22-Sep-20 8:14am    
"Now when I click on it" click on what ? Show the method signature of your code: so we know how e is defined.
Member 14085040 23-Sep-20 3:32am    
@BillWoodruff. Hi. I have a gridview where one of the column must have value from the database. The value is 4 digits of numbers (Kontonummer). I made an AutoComplete TextBox for it. In the Textbox itself I have multiple columns (Kontonummer + Navn). In the cell when I test a number, however, the suggestions from DB in the Textbox come both (Kontonummer and Navn). When I click on a row in the Textbox, both (Kontonummer and Navn) enter the cell, this gives error as the column's properties are (int) and I need to have only (Kontonummer) value returned to the cell. Hope that is better explanation.
BillWoodruff 23-Sep-20 5:08am    
you are repeating the same message, and not responding to the specific questions you are asked, and information provided. we can't help you if you do this,
Richard Deeming 22-Sep-20 10:43am    
new SqlDataAdapter("SELECT CAST(Kontonummer AS nvarchar (255)) + ' - ' + Navn AS Kontonummer FROM Kontoplan WHERE LockKonto = 'True' AND KlientId = (SELECT Id FROM Klient WHERE Navn = '" + bogf.cmbKlient.Text + "')", con)

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

SqlDataAdapter da = new SqlDataAdapter("SELECT CAST(Kontonummer AS nvarchar (255)) + ' - ' + Navn AS Kontonummer FROM Kontoplan WHERE LockKonto = 'True' AND KlientId = (SELECT Id FROM Klient WHERE Navn = @Navn)", con);
da.SelectCommand.Parameters.AddWithValue("@Navn", bogf.cmbKlient.Text);
ZurdoDev 22-Sep-20 12:13pm    
What's the error?

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