Click here to Skip to main content
15,564,534 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Iv looked around, and found many solutions, but they either dont make enough sense to apply to my specific issue, or they dont work.

Heres what I have:
1 form that holds a datagridview, that is supposto contain a bunch of usernames and passwords.
This form is never seen by the user.
Another form - in which the user sees, that asks them for a username and password, then a "Log In" button.

Upon pressing the button, the program runs through every item on the datagridview until it finds the exact username you typed in, if it finds it, it sees if the password attached to that username matches, if it cannot find the username, or the password doesnt match, it returns a error to the user saying "the username or password you entered was not found" (depending on which field as wrong)

That all works fine.
But... For whatever reason I cant access this database, unless I access it programmically...

So, heres my problem:
I added a new row to the datagridview, before it looks through it (because for whatever reason it would never save the items, so its alaways -1 rows in the database), then I rigged it for that first field (Username field) to be "Bob" for example - then I had it check, if the username they entered (Again, Bob for example), is the same as the name in the database, then say your logged in, otherwise, say "failed".

What it does is, its crashing saying the following error:
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

So, my question:
How do I either 1.
Unbind this data-bound control?
or 2.
Edit the rows without alot of complicated code?

Here is the code I have now:
VB
Form2.DataGridView1.Rows.Add(1)
Form2.DataGridView1.Rows(0).Cells(0).Value = "Bob"
If Textbox1.Text = Form2.DataGridView1.Rows(0).Cells(0).Value Then
    MessageBox.Show("Logged In.")
Else
    MessageBox.Show("Failed.")
End If



Could anyone give me some sample code aswell? That would be greatly appreciated.

Also, the datagridview is never edited during runtime, and I cant manually add items to it in the design view.
Posted
Comments
Akinmade Bond 7-Oct-13 16:17pm    
Why not check if the username directly exists in your database directly rather than checking for in on another form.

If you're never display the DataGrid, then don't use a DataGrid. The whole purpose of a DataGrid is for displaying. It's going to add a lot of overhead to get your form to load, and generally complicate stuff, and you just don't need to. Instead, just use a DataTable[^].

Also, there is no reason to pull ALL of the user ids and passwords. Don't pull the data until after the user has typed the user id and password. Then pull data from the database with an SQL WHERE clause that looks just for the user id that was typed in. (Use parms to do this.) If it doesn't pull anything, then you know that user id isn't valid. After it's pulled you can check password stuff. Pulling ALL of the user IDs and passwords makes your program slower and just doesn't seem as secure.

If you need more help with how to pull the data and use parms, please specify what type of database you are using.

Hope this helps.
 
Share this answer
 
Comments
School Projects 8-Oct-13 7:55am    
Thanks.
Well, I guess I wasnt too clear, the user can never see this datagridview, but if they use the username "Admin" and the password "letMein!" then they have access to the 2nd form that contains the datagrid, and they are able to add,edit, and delete users from it - thats just how I do it manually, but I also want to do it pragmatically, so that isnt needed.

The method you explained might also be a good solution, so ill try it out, and let you know if I run into any problems, thanks.
Hi,
What Kschuler says is correct, you should never get so much data from your DB.
If you are trying to update your gridview that is databound then you should Update your DB
as well, before using data from the gridview.

If it does not update your data in the DB like you said,
try looking at the way you update your DB.
For the EF for instance you have to attach the object to the object from the DB and then
specify .saveChanges ...

Tom
 
Share this answer
 

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