Bindingsource and a SQL Query?

Jelmer

Well-known member
Joined
Jan 11, 2006
Messages
96
Hello

On a form ive got these components:

datagridview
dataset
bindingsource
FillbyToolstrip

ive configured all things and it works.
But i like to add a button to the form, and if i press it that there is executed another query. So... is it possible to change the FillByToolstrip with a code? so i can use another SQL Query ?

thanx,

Jelmer
 
Hi,

Im guessing youre using the wizards to make that but, programmatically you can create a new DataSet and a new Command and a new DataAdapter; you specify your sqlquery associate it wit hthe command, fill the dataset with that command, and just bind the dataset to your existing bindingsource as:

BindingSource myBindingSource = new BindingSource();
myBindingSource.DataMember = "TableName";
myBindingSource.DataSource = myDataSet.Tables["TableName"];
myBindingSource.ResetBindings();

And it should work fine

.eof
Teixeira
 
Hi,

Im guessing youre using the wizards to make that but, programmatically you can create a new DataSet and a new Command and a new DataAdapter

Is that the best way to do it? Creating multiple datasets and dataAdapters doesnt sound good to me (but Im not an expert :) ).

What I want to do is just set my SQL query and then make the bindingsource navigate only throught the query result, not the whole table. (since Im using WHERE statements).

Im using wizard to bind it to TextBoxes and DataGridViews. The Fillby gives me the option to select a query, and its in its command text property, but I want to change the given some values, not in a wizard. But when you bind a textbox, you dont use a Fillby (I think).

Your response didnt completely answer my question. I would like to ask you to elaborate it a little more (just a little bit). Im new to .NET and SQL Server and this ADO.NET thing. :) Maybe if you could start your response once somebody used the wizards to create the bindingSource (or maybe how to make a BindingSource without using the wizards at all; I dont know whats the best way).

If anybody else could give me some insight, I will appreciate it too.:D

Thanks.
 
I have found the answer to my own question. I got a VERY useful example doing exactly what I wanted to do. Except that it doesnt use the BindingSource, but thats OK.

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=6149&lngWId=10

The code is SUPER easy to understand. It has helped me a lot, and its approach is very similar to what I wanted to do. I really want to send an email to that guy

I have one problem here. Take a look at this query.

Code:
cmd.CommandText = "Delete from PersonInfo where (FullName = N" & txtFullName.Text & ")"

After FullName, there is an N. I dont know what is it for, or what does it do.

The only weird thing the programm does is that it adds a number to a combobox, everytime you try to add a new record, but it keeps a count somewhere outside the .NET application (since I couldnt find a declaration for N or a counter inside the code). Maybe it is in the database.

For instance, lets say it has 10 records and then I add a new one. Then a 11 appears at the combobox. If I delete the 11 record and try to ad a new one, an 12 appears, instead an 11, despite the fact the 11 no longer exists.

What is happening here or how is he doing it?
 
The N specifies the string that follows in single quotes is a unicode string rather than an ansi string.

It sounds like the table in the data base has an Identity column - basically it just increments for every record inserted.

The only other thing I would say is thet the example you posted is open to a potential security risk and you would be much better off using a parameterised query rather than relying on string concatenation.
 
The only other thing I would say is thet the example you posted is open to a potential security risk and you would be much better off using a parameterised query rather than relying on string concatenation.

Really? :eek: What are those risks? And whats a parameterised query? :p

BTW, Im not using ASP, this is a desktop application. :cool:
 
Last edited by a moderator:
Edit: Nevermind, I solved my problem. :)

BTW, just a question. I had to add Parameters.Clear() method after calling it for the first time, because on the second time, an error pop ups saying that you cant declare variables twice. Is this normal?
 
Last edited by a moderator:
Back
Top