Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys, I was working on c# win form but I leave this project for some time now today when I open my project and I add a form to my application and when I run
so am getting this error
Additional information: There is no row at position 0.

but in my DB I have 60000 data of this table I don't know what the issue please guys help me
Thanks

What I have tried:

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBox2.Text == "")
            {
                return;
            }
            else
            {
                con_string.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\Restaurant.accdb;Persist Security Info=False";
                con_string.Open();
                DataSet dsa = new DataSet();
                DataTable dt = new DataTable();
                dsa.Tables.Add(dt);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da = new OleDbDataAdapter("SELECT [Place Name],[State],[Country],[Latitude],[Longtitude] FROM [Address] where [Postal Code] = " + textBox2.Text + "", con_string);
                da.Fill(dt);
                if (dt.Rows[0][0] =="")
                {
                    MessageBox.Show("User Cannot be Found", "Change Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                }
                textBox3.Text = dt.Rows[0][0].ToString();
                textBox5.Text = dt.Rows[0][0].ToString();
                textBox4.Text = dt.Rows[0][0].ToString();
                con_string.Close();
            }
        }
Posted
Updated 24-Jan-18 20:02pm

1 solution

For starters, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And second, if there are no rows that match the postcode, no rows will be returned. If no rows are returned, then any attempt to access them will give you a "There is no row at..." exception. Check the Rows.Count property instead, and don't even try to fill your textboxes if there are no rows.

Now for the stuff you haven't noticed...
1) Text boxes 3, 4, and 5 will all get the same data.
2) If the user types wrong, don't clear his input! Select it, and set the focus to that control yes - he then knows what is wrong and can fix it. But deleting what he typed so he can't see what he did wrong is just rude.
3) Don't hardcode connection strings - it's a real pain when you move to production. Always use a config file or similar.

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
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