Click here to Skip to main content
11,484,162 members (66,885 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0
I tried to make a autoComplete textbox like google Search with C# in a WPF application, basically what I want to do is have a autocomplete textbox which is bound to a sql database table. the table has 2 fields(Barcode and Name),my code as below:

In XMAL :

 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="37*" />
            <RowDefinition Height="88*" />
        </Grid.RowDefinitions>
        <TextBlock Text="Type Your Search :" HorizontalAlignment="Left" 
                   VerticalAlignment="Bottom" Width="112" Height="15.96" Margin="31,0,0,4" />
        
        <TextBox HorizontalAlignment="Right" VerticalAlignment="Bottom" 
                 Height="25" Width="325" Margin="0,0,10,0" x:Name="txtCAuto" TextWrapping="NoWrap" />
        
        <ListBox x:Name="lbSuggestion" SelectionChanged="lbSuggestion_SelectionChanged"
            removed="LightYellow" Grid.Row="1" Visibility="Collapsed" 
                 HorizontalAlignment="Right" VerticalAlignment="Top" Width="325" Margin="0,0,10,0"/>
    </Grid>
Code Behind:
        List<string> nameList;
        List<Product> prodList;
 
        public List<string> SelProd4Sale(string str )
        {
            string constr = "Data Source=.;Initial Catalog=AgamistaStore;User ID=emad2012;Password=emad_2012";
            SqlConnection SqlCon = new SqlConnection(constr);
            SqlCommand SqlCmdProds = new SqlCommand();
            SqlCmdProds.Connection = SqlCon;
            SqlCmdProds.CommandType = CommandType.Text;
            SqlCmdProds.CommandText = "SELECT dbo.ProductsTbl.ProductID,ProductsTbl.ProductBarcode," + 
                "dbo.ProductsTbl.ProductName, dbo.ProductsTbl.SalePrice FROM dbo.ProductsTbl ";
            SqlCon.Open();
            SqlDataAdapter dapProds = new SqlDataAdapter();
            dapProds.SelectCommand = SqlCmdProds;
            DataSet dsProds = new DataSet();
            dapProds.Fill(dsProds);
            SqlCon.Close();
            prodList = new List<Product>();
            for (int i = 0; i < dsProds.Tables[0].Rows.Count; i++)
            {
                prodList.Add(new Product
                                (dsProds.Tables[0].Rows[i]["ProductBarcode"].ToString(),
                                dsProds.Tables[0].Rows[i]["ProductName"].ToString());
            }
            dsProds = null;
 
            nameList = new List<string>() 
            {
               prodList.ToString()
            };
            
            return nameList;
        }
 
        public Window2()
        {
            InitializeComponent();
            SelProd4Sale(txtCAuto.Text);
            txtCAuto.TextChanged += new TextChangedEventHandler(txtAuto_TextChanged);
        }
 
        #region TextBox-TextChanged-txtAuto
        private void txtAuto_TextChanged(object sender, TextChangedEventArgs e)
        {
            string typedString = txtCAuto.Text.ToUpper();
            List<string> autoList = new List<string>();
            autoList.Clear();
 
            foreach (string item in nameList)
            {
                if (!string.IsNullOrEmpty(txtCAuto.Text))
                {
                    if (item.StartsWith(typedString))
                    {
                        autoList.Add(item);
                    }
                }
            }
 
            if (autoList.Count > 0)
            {
                lbSuggestion.ItemsSource = autoList;
                lbSuggestion.Visibility = Visibility.Visible;
            }
            else if (txtCAuto.Text.Equals(""))
            {
                lbSuggestion.Visibility = Visibility.Collapsed;
                lbSuggestion.ItemsSource = null;
            }
            else
            {
                lbSuggestion.Visibility = Visibility.Collapsed;
                lbSuggestion.ItemsSource = null;
            }
        }
        #endregion
 
        #region ListBox-SelectionChanged-lbSuggestion
        private void lbSuggestion_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbSuggestion.ItemsSource != null)
            {
                lbSuggestion.Visibility = Visibility.Collapsed;
                txtCAuto.TextChanged -= new TextChangedEventHandler(txtAuto_TextChanged);
                if (lbSuggestion.SelectedIndex != -1)
                {
                    txtCAuto.Text = lbSuggestion.SelectedItem.ToString();
                }
                txtCAuto.TextChanged += new TextChangedEventHandler(txtAuto_TextChanged);
            }
        }
        #endregion
    }
 
    class Product
    {
        private string _ProductBarcode = "";
        private string _ProductName = "";
 
        public Product(string prodName,string prodBarcode)
        {
            this._ProductBarcode = prodBarcode;
            this._ProductName = prodName;
        }
 
        public string ProductBarcode
        {
            get { return _ProductBarcode; }
            set { _ProductBarcode = value; }
        }
 
        public string ProductName
        {
            get { return _ProductName; }
            set { _ProductName = value; }
        }
 
    }
When I run this I got "System.Collections.Generic.List" as result instead of data.

Can somebody help me please & tell me what 's wrong?

Thanks.
Posted 13-Dec-12 8:10am
Edited 13-Dec-12 8:14am
v7
Comments
richcb at 13-Dec-12 13:17pm
   
ProgramFOX is correct, you are getting the ToString() of your list, not an actual item. I would suggest finding a property or another way to get the value you want.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I see this line of code:
prodList.ToString();
List.ToString(); returns always "System.Collections.Generic.List".
I can't tell you what to do, because I don't know which result you want to get instead of "System.Collections.Generic.List".

[EDIT]

To show the product name, change this statement:
nameList = new List<string>()
{
   prodList.ToString()
};
Into this:
nameList = new List<string();
foreach (Product pr in prodList)
{
    nameList.Add(pr.ProductName);
}
[/EDIT]

Hope this helps.
  Permalink  
v3
Comments
Doudy_2020 at 13-Dec-12 13:27pm
   
I want to show "product name" as a result when the user type barcode in the textbox.
ProgramFOX at 13-Dec-12 13:40pm
   
Ok. I updated my answer.
Doudy_2020 at 13-Dec-12 14:41pm
   
I try your idea, but it does't retrieve any data
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I made little adjustment to your code.

From this to
for (int i = 0; i < dsProds.Tables[0].Rows.Count; i++)
{
    prodList.Add(new Product
               (dsProds.Tables[0].Rows[i]["ProductBarcode"].ToString(),
               dsProds.Tables[0].Rows[i]["ProductName"].ToString());
}
dsProds = null;
nameList = new List<string>()
{
  prodList.ToString()
};
return nameList;

This.
nameList = new List<string>();
for (int i = 0; i < dsProds.Tables[0].Rows.Count; i++)
{
    nameList.Add(dsProds.Tables[0].Rows[i]["ProductName"].ToString())
}
dsProds = null;
return nameList;
</string>
  Permalink  
Comments
Doudy_2020 at 13-Dec-12 14:41pm
   
I try your code, but it does't also retrieve any data
jibesh at 13-Dec-12 14:45pm
   
"but it does't also retrieve any data"

It doesnt mean that code doesnt work. check your query returning any values. Have you tried debugging your application?
check your SQL query is working or not and it returns any rows.

what is the value of dsProds.Tables[0].Rows.Count ?
Doudy_2020 at 15-Dec-12 7:32am
   
SQL query is working fine & it returns data, & when I debug my application it works fine with no error, & I got this data, but it doesn't show in listbox
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

A tip is to include ItemTemplate and ContentTemplate to your ListView and manipulate displayable data there.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,

As ProgramFOX suggested,
nameList = new List<string();
foreach (Product pr in prodList)
{
    nameList.Add(pr.ProductName);
}
is correct.

Then have to focus on the following section,
foreach (string item in nameList)
{
    if (!string.IsNullOrEmpty(txtCAuto.Text))
    {
        if (item.StartsWith(typedString))
        {
            autoList.Add(item);
        }
    }
}
If SQL query is correct and returns data then check whether elements in nameList. If so check whether you reach autoList.Add(item) or not.

Regards.
  Permalink  
Comments
Doudy_2020 at 15-Dec-12 10:54am
   
SQL query is correct and returns data, & elements in namelist @debug mode have data also autoList has data, but lbSuggestion.ItemsSource does't have any data
And it also retrieve data in list for example if they are 4 products start with letter "p", the listbox show 4 rows but data hidden & when I select any row I got it in the textbox

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

  Print Answers RSS
0 Maciej Los 190
1 Sergey Alexandrovich Kryukov 170
2 Raje_ 155
3 OriginalGriff 147
4 Mehdi Gholam 120
0 Sergey Alexandrovich Kryukov 8,484
1 OriginalGriff 7,948
2 Sascha Lefèvre 3,189
3 Maciej Los 2,601
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 16 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100