how to change starting position combobox C# mysql - c#

I have a little trouble using C# combobox with MySql
So, to begin with... I have combobox and button on a form. After submitting a form, executes MySql query like :
"select * from db1.users where login='"+combobox.SelectedIndex+"')";
It works great, But.... if we submit the first element of combobox - MySql FOREIGN KEY problem occures (= NULL and so on).
Any Ideas how to give him know, that first element from combobox isn't NULL ?

Stupid idea but who knows:
Mysql counts from 1, combobox from 0 did you try:
(selectedIndex + 1)

I'll assume you're talking about just submiting the button without actually selecting a value on the combobox, right?
You should do a simple if before executing your command, like:
if (combobox.SelectedIndex != null)
{
codethis("select * from db1.users where login='"+combobox.SelectedIndex+"')");
}
else
{
//do something else
}

Are you sure what you want isn't combobox.SelectedItem?
This would be the case if "login" was a string in the SQL database.
If it's an integer, have you tried doing combobox.SelectedIndex+1? This could fix the problem if "login" starts at "1" in the SQL whilst SelectedIndex starts at 0 in C#.
In any case, check the answer below if your problem is the combobox having no selected item.
Optionally you can have the form OnLoad do combobox.SelectedItem=1;

You would want to stop using SelectedIndex, because it's not meant to represent your data. The problem may not even be in your current situation (mismatch between 1s and 0s indexing). Consider the following:
Another developer adds sorting to your combox. Now instead of a,b,c the values are c,b,a and the selected index of 0 will now point to c instead of a as it has used to do
Somebody removes users in the database, so in the database you now have b,c,d, think about what data you will have in your combobox?
To fix this problem you should use a value. Each combobox should have: displayed text, corresponding value (which is hidden from user) and selected index. Using selected index (or selected item) you get the item's value which should be your database_id. For example see this answer if the value is not available for your combobox type out of the box.
Bonus points:
use parameterised queries (prevents SQL injection attacks)
never store passwords, instead store password hashes and random salts (read more)

Related

ASP.net returning value from sql proc in an if statement

i have a login box on my form with a submit button , on submit im passing the value through to sql to return a simply yes or no.
I can see the value No as my outcome (pic 1)
however i cannot seem to pull it though to my If statement (pic 2). it only allows my to choose count which is always 1, i need the Yes or no value. something on the lines of
if (Login.Outcome() == "No" )
I have read somewhre its within items but need help on how to get this value.
enter image description here
enter image description here
Your screenshot seems to indicate that Login is a collection whose [0] element has an Outcome property?
if(Login[0].Outcome == "No")

C# DataGridView: Selected Row always returns First Row (Regardless of Selection)

I have a DataGridView in which the User selects a Row, and presses a Button which acts based off of the Selected Row.
Currently, I am trying to get the ID (First Column) of the Selected Row using the following method:
int id = (int) DataGrid_Contact.SelectedRow[0].Cells[0].Value;
I have tried other methods, such as dgv.CurrentRow, dgv.SelectedCells, etc. Everything always points to either the First Row, or the First Cell (0, 0). Regardless of my selection, I cannot get this to change.
The notable properties of the DataGridView are:
MultiSelect = false;
ReadOnly = true;
SelectMode = FullRowSelect;
Everything else is either unrelated to Selection and/or set to their default values.
In case it matters, I am populating the DataGridView with an SQL Command, and setting the DataSource of the DataGridView. I've tried this.BindingContext[DataGridView dgv.DataSource].EndCurrentEdit() to no avail.
Lastly, I'm using Microsoft Visual C# 2008 Express Edition and Microsoft SQL Server 2008.
after analysing your code i have observed following mistakes from your code:
1.you are directly casting the object value to int :
Solution: here you need an explicit cast.
2.you are trying to get the selected Row items using following statement:
DataGrid_Contact.SelectedRow[0].Cells[0].Value;
actually above statement won't be compiled as there is no SelectedRow[int] Collection.instead of that you should use SelectedRows[int] Collection as below:
DataGrid_Contact.SelectedRows[0].Cells[0].Value;
3.from your properties it is clear that you have disabled the MultiRowSelect so there is no point of getting data from multiple rows.
Final Solution:
Replace this :
int id = (int) DataGrid_Contact.SelectedRow[0].Cells[0].Value;
With following:
int id = Convert.ToInt32(DataGrid_Contact.SelectedRows[0].Cells[0].Value.ToString().Trim());
You should be able to pull the index of the selected row and then use the original structure to get the proper element.
objList[yourGrid.SelectedIndex]
I figured all of this out.
First of all, there was a typo. SelectedRow should have been SelectedRows.
Also, my cast worked just fine.
Lastly, the issue is that I had set the menu to be invisible before grabbing my selected data. For whatever reason, when the window is invisible the selection defaults to 0, 0 rather than staying at its previous value.

Cannot select multiple items in a dropdownlist

My C# ASP.NET web site has a weird problem.
I set a selected value for a dropdownlilst based on a value from a stored procedure output parameter like this:
this.myDropDown.SelectedValue = cmd.Parameters["#SourceID"].Value.ToString().Trim();
For some reason, I get a "Cannot have multiple items selected in a dropdownlist" error on this line of code. I've stepped through the code and searched for other references to this dropdownlist, commented the references out, and tried again.
Nope, still doesn't work.
The only way the page works is if I comment out the above line of code. Any ideas why this would be a problem?
SourceID is always an integer and exists in the list of selections. I've verified (by stepping through the code) that the selectedValue is always numeric, and never changes between the time this line executes and the time the page finishes loading.
Thoughts?
This would occur if you had two items in the DropDownList with the same Value.
If you do mean to select multiple items, you need to use ListBox control with SelectionMode="Multiple" set.
You have 2 items with the same value.
You can solve it:
1) Find Items by value to list.
2) Get first item index.
3) Select item by index.

How to stop entering the text, when user going out of autoCompleteMode text c#

Iam using AutoCompleteMode to my textbox. It will append the Bank names to my textbox. So when I started typing with the first leter all the Bank names with first lettre will come to dropdownlist to my textbox. Now my question is
If user try to entre the data which is not im my dropdownlist, the user should not able to entre the text. So i want user to entr the existing bank names only.
Iam using AutoCompleteCustomSource to the textbox for dropdown.
try something like:
bool foundSome = false;
foreach (var bankName in col)
{
foundSome = bankName.StartsWith(textBox.text);
}
if (foundSome)
//Do some action
You can write this code in 'Validating' to preform for each char inserted in txtbox.
the best way to achieve your requierement is to use 1 textbox and 1 combobox. They both should point to the same collection.
Textbox will behave in autocomplete mode as you described. Once you type, combobox value will be set to first matching value from your collection. If no value matches - combobox value should be set to null or default data.
Combobox will store only corresponding data subset with no possibility to edit the chosen text.
Validation and data retrieval will be done from Combobox value.
Advantages of this approach:
- With large sets of data it will be easier for user to find what he/she needs.
- Lesser code to check if input value belongs to data set or to force belonging.
- No validation is needed.
Possible shortcomings:
- One more control at form.
- Logic to synchronize textbox's text and combobox collection should be implemented.

Reading back ListBox values and setting default

I'm new to this site, and basically I have created a Windows Form using C#.
On this form I have 3 values.
I have a button to access the values in my ListBox.
How could I make it so when the form loads, the first element in my list box is highlighted?
I was also wondering is there a way to start the indexing off for 1, if the first element in my list is selected? rather than 0?
I hope I have stated this as clearly as I should,
To select the first index, add this line to the OnLoad event of the form:
myListBox.SelectedIndex = 0;
As for your second question about changing the indexing range, the answer is no. You could use 1 to whatever, but then you would have to use -1 after. Like so:
myListbox.Items.Remove(Your_Value - 1);

Categories