Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
greetings,

i have a code like this;

C#
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TESTDB;Integrated Security=True");
            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Project where Info= '" + textBox1.Text + "'", con);
            DataTable dt = new DataTable();
            
            sda.Fill(dt);
            if (dt.Rows[0][0] != DBNull.Value)
            {
                textBox1.Text = dt.Rows[0][0].ToString();
                textBox2.Text = dt.Rows[0][1].ToString();
                textBox3.Text = dt.Rows[0][2].ToString();
                textBox4.Text = dt.Rows[0][3].ToString();
            }

            else
            {
                MessageBox.Show ("Please Check Info!");
            }


i have 4 rows on my sql table. i'm writing necessary info on textbox1 and than pressing a button. After than other textboxes gets data directly from sqlserver. But when i write a info that is not including in dt.Rows[0][0], my code crushes. please check my if condition and tell me what is wrong about it. How can i write this exception?
Posted

First off:
Understanding SQL Injection and Creating SQL Injection Proof ASP.NET Applications[^]

Never create queries from user input.

Have you met my Dad? His name is '; drop table *;'

Ok - dt.Rows may be empty so dt.Rows[0] will be an "out of bounds" error.

Add a check around it:

C#
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TESTDB;Integrated Security=True");

//Paramterized query.  Much safer!
SqlCommand com = new SqlCommand("SELECT * FROM Project where Info=@param", con);
com.Parameters.Add(new SqlParameter("@param", SqlDbType.VarChar) { Value = textBox1.Text });
SqlDataAdapter sda = new SqlDataAdapter();

DataTable dt = new DataTable();

sda.Fill(dt);
//If the first check fails then it will not process the second
if (dt.Rows.Count > 0 && dt.Rows[0][0] != DBNull.Value)
{
    textBox1.Text = dt.Rows[0][0].ToString();
    textBox2.Text = dt.Rows[0][1].ToString();
    textBox3.Text = dt.Rows[0][2].ToString();
    textBox4.Text = dt.Rows[0][3].ToString();

}
else
{
    MessageBox.Show("Please Check Info!");
}
 
Share this answer
 
Comments
OriginalGriff 20-May-15 6:47am    
I think your father changed his name, didn't he?
I'm sure he became
';DROP TABLE *;--
Andy Lanng 20-May-15 6:50am    
Oh yes, but I always forget the "new age" '--' he added after his mid-life crisis. He's such a hippy.
ROFL

I'll just post the xkcd link next time:
https://xkcd.com/327/
OriginalGriff 20-May-15 6:56am    
:grin:
I also save this one:
http://www.commitstrip.com/wp-content/uploads/2013/09/Strips-Erreur-au-pilori-001-650-finalenglish.jpg
for other offenders...
Andy Lanng 20-May-15 6:58am    
Ha ha - love it.

There should be an article page for the best comics, including that one with the tree swing from the point of view of different stake-holders :)
Richard Deeming 21-May-15 11:15am    
The connection and command should be wrapped in using blocks, and you should probably stick a TOP 1 in the query as you're only using the first row.

Otherwise, a perfect answer. :)
Try this instead:
C#
if (dt.Rows.Count > 0)
   {
   ...

You could use:
C#
if (dt.Rows.Count > 0 && dt.Columns.Count > 0)
   {
   ...
But it shoudln't be necessary.
 
Share this answer
 
Comments
hypermellow 20-May-15 6:36am    
... beat me to it! +5
Andy Lanng 20-May-15 6:42am    
Please remember to moan about SQL Injection. I fear how many sites have these issues :S
OriginalGriff 20-May-15 6:46am    
I normally do...forgot this time...:doh:
Hi,
It's good practice to check if your data calls actually return data before trying to use any of it.

In your instance, checking that your DataTable contains data would be the place to do this. A simple test would be to check the DataTable.Rows.Count property is greater than zero.

Something like:
C#
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TESTDB;Integrated Security=True");
             SqlCommand com = new SqlCommand("SELECT * FROM Project WHERE Info=@InfoParam", con);
            com.Parameters.AddWithValue("@InfoParam", textBox1.Text);
            DataTable dt = new DataTable();
            
            sda.Fill(dt);
            if ((dt.Rows.Count>0)&&(dt.Rows[0][0] != DBNull.Value))
            {
                textBox1.Text = dt.Rows[0][0].ToString();
                textBox2.Text = dt.Rows[0][1].ToString();
                textBox3.Text = dt.Rows[0][2].ToString();
                textBox4.Text = dt.Rows[0][3].ToString();
            }
 
            else
            {
                MessageBox.Show ("Please Check Info!");
            }


... hope it helps.
 
Share this answer
 
v2
Comments
Member 10587827 20-May-15 8:02am    
thank you :)
Richard Deeming 21-May-15 11:13am    
You've copied the SQL Injection[^] vulnerability from the question.
hypermellow 21-May-15 11:28am    
*Hangs his head in shame* ... I really should have picked up on that.
hypermellow 21-May-15 11:49am    
... now updated, Thanks.
if(dt != null && dt.Rows.Count > 0)
{
.....
.....
}
 
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