Get the code from List Box

Stimulsoft Reports.NET discussion
Post Reply
samueldearaujo
Posts: 10
Joined: Tue Oct 15, 2013 7:49 pm
Location: Itaúna, Minas Gerais, Brasil

Get the code from List Box

Post by samueldearaujo »

Hello!

How do i to get the code of selected item on ListBox?
Let me mean what are happening.
In my report, I have a form where the user should find the name of your client by inserting his name for find it. (Some parts of him).
The result is showing in a list below it. So, he needs to select the appropriate result for continues showing the report.
To here, all right. But, when the user select a result in list and presses ok for continue. On report, the result isn't correct. Sometime the report shows the data from the first item of the list box, or the last.

Follows some pictures:
Fist test:
formp1.PNG
formp1.PNG (16.66 KiB) Viewed 2923 times
Result:
result1.PNG
result1.PNG (6.13 KiB) Viewed 2923 times
Second test:
formp2.PNG
formp2.PNG (17.59 KiB) Viewed 2923 times
Result:
result2.PNG
result2.PNG (7.66 KiB) Viewed 2923 times
The code of search button:

This part, i think that works, because the List Box is filled correctly.

if(!txt_Cliente.Text.Equals(String.Empty)){
Boolean c = false;
if(check_Cliente.Checked == true){
NomeCliente = "%" + txt_Cliente.Text + "%";
c = true;
}else if (check_Codigo.Checked == true){
long codteste;
if(long.TryParse(txt_Cliente.Text.Trim(), out codteste)){
CodigoCliente = codteste;
c = true;
}else{
MessageBox.Show("Código inválido.");
}
}

if(c){
PesquisaCliente.Connect();
object [] values = Stimulsoft.Report.Dictionary.StiDataColumn.GetDatasFromDataColumn(this.Dictionary, "PesquisaCliente.NOME");
Array.Sort(values);
list_Horarios.Items.Clear();
list_Horarios.Items.AddRange(values);

}
}


The code of button 'Ok':


Boolean sucess = false;
if(list_Horarios.SelectedIndex != null && list_Horarios.SelectedIndex != -1){
long codCliente;
if(long.TryParse(PesquisaCliente.GetData("CODIGO", list_Horarios.SelectedIndex).ToString(), out codCliente)){
CodigoCliente = codCliente;
Parametros = " WHERE P.pk_codPessoa = @CodigoCliente ORDER BY DATASESSAO ";
sucess = true;
}
}

if(sucess){
this.btn_Ok.DialogResult = System.Windows.Forms.DialogResult.OK;
ConsultaAgenda.Connect();
}

But, here. The code isn't right.
For instance:
The code of the selected result in ListBox, is 7 on database, but is being 299.

The Dictionary:
dictionary.PNG
dictionary.PNG (8.56 KiB) Viewed 2923 times
Last:
The query is right, only the code that it receives is that wrong.

I hope that you understand. :D
Thanks
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Get the code from List Box

Post by Alex K. »

Hello,

Can you please send us a sample project which reproduce the issue for analysis.

Thank you.
samueldearaujo
Posts: 10
Joined: Tue Oct 15, 2013 7:49 pm
Location: Itaúna, Minas Gerais, Brasil

Re: Get the code from List Box

Post by samueldearaujo »

Hi!

Follows the files for analisys:
A project, a mrt file and a sql script.
For your tests, search by 'Sam', as the last message.

By the way.
Is it possible to close the form without open the report, when you press the esc key?
Or, how to close the form without opening the report by clicking on the cancel button?
Generally, even clicking in cancel button, the report opens blank.
Attachments
Relatorios.rar
(4.81 MiB) Downloaded 212 times
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Get the code from List Box

Post by Alex K. »

Hello,

Please check your query or data.

Thank you.
Attachments
Capture.PNG
Capture.PNG (111.6 KiB) Viewed 2888 times
samueldearaujo
Posts: 10
Joined: Tue Oct 15, 2013 7:49 pm
Location: Itaúna, Minas Gerais, Brasil

Re: Get the code from List Box

Post by samueldearaujo »

Hi Aleksy!

Is that, but almost right.
If you repair, you selected another name, the name ‘Samuel de Araujo’ and the form returns the id (codigo) 299. This id refers to ‘Samuel Vieira’ another person.
So, this is the problem.
1.png
1.png (182.49 KiB) Viewed 2884 times
In another example,i selected the name ‘Samara’ and the form returns me the correct index, but the id isn't right.
The id 132 is refers to name ‘samanta 031 o 2º’ and not to ‘Samara’.
2.jpg
2.jpg (136.37 KiB) Viewed 2884 times
Here the query result in Sql Server.
3.jpg
3.jpg (107.95 KiB) Viewed 2884 times
I want to know if this code is right. Look:
if(long.TryParse(PesquisaCliente.GetData("CODIGO", list_Horarios.SelectedIndex).ToString(), out codCliente)){

CodigoCliente = codCliente;
Parametros = " WHERE P.pk_codPessoa = @CodigoCliente ORDER BY DATASESSAO ";
sucess = true;
}
Is anything wrong?

In C# I can use ListView.DataSource to bind the object with the query's result and use the .DisplayMember and .ValueMember for displaying names and values's relation.
So, i can retrieve the value selected by:
"long c = long.parse(listview.SelectedValue.ToString())" for instance.

Is it possible use this in Stimulsoft Reports?
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Get the code from List Box

Post by HighAley »

Hello.

The problem is in the next line:

Code: Select all

Array.Sort(values);
You get the list of names and change its order. Then you look for the name by its index in the Data Source that has different order. If you remove this line you will see the right name.
As a way, please, add order by statement to your SQL query.

Thank you.
samueldearaujo
Posts: 10
Joined: Tue Oct 15, 2013 7:49 pm
Location: Itaúna, Minas Gerais, Brasil

Re: Get the code from List Box

Post by samueldearaujo »

Hi Aleksey!
You're right, it's only this. Problem solved.
I used your suggestion to add "order by" on SQL query.
Thank you very much!
And i'm sorry for this inconvenience.
Andrew
Posts: 4109
Joined: Fri Jun 09, 2006 3:58 am

Re: Get the code from List Box

Post by Andrew »

Hello,

Thank you for letting us know about this.

No need to apologize. It is our work to help you).

Have a nice day!
Post Reply