Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing a desktop application in c# where i got this error while debugging my program .
I want to send the data from window application to the database of web application .And I connected the web application database.This is the second form it contain one more form e.i login form. So please kindly help me

What I have tried:

C#
public void timer1_Tick(object sender, EventArgs e)
{
    tik--;
    if (tik == 0)
    {
        timer1.Stop();
        string constring = @"Data Source=(LocalDB)\v11.0;Initial Catalog=D:\WEBSITE10\APP_DATA\INFO.MDF;Integrated Security=True";

        SqlConnection cnn = new SqlConnection(constring);
        try
        {
            string q = "update imagetable set Image_Capture = @pic where email = @Eml";
            SqlCommand scmd = new SqlCommand(q, cnn);
            scmd.Parameters.AddWithValue("@Eml", this.textBox2.Text);
            if (pictureBox1.Image != null)
            {
           MemoryStream     ms = new MemoryStream();
                pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
                byte[] photo_array = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(photo_array, 0, photo_array.Length);
                scmd.Parameters.AddWithValue("@pic", photo_array);

            }
            if (cnn.State != ConnectionState.Open)
                cnn.Open();
            SqlDataReader dr = scmd.ExecuteReader();
          //  cnn.Close();
            webcam.Stop();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
Posted
Updated 21-Apr-16 4:04am
v4
Comments
Kevin Marois 20-Apr-16 14:22pm    
Your SQL statement(s) are probably incorrect. Paste them into SSMS and see what happens.
Member 12399485 20-Apr-16 14:35pm    
this is working properly
Kevin Marois 20-Apr-16 14:47pm    
You posted way too much code here, and it's not formatted. Can you post just the part that's failing?
Richard Deeming 20-Apr-16 15:09pm    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

EDIT: I see you've fixed that - well done! :)

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
Sinisa Hajnal 21-Apr-16 2:35am    
Too much code to read. Post the relevant code and format it. In general, check your connection string (make sure it connects), user rights and then check the queries you're sending to the database. Move them into stored procedures, execute them in query analyzer (not what you think you're sending, but take the string from the program).

Also, don't concatenate user input or you might find yourself without database to work with.

1 solution

There are a number of things to note here: This is a timer Tick event handler, so the chances are that it occurs quite often, and quite frequently. And you don;t close or Dispose your SQL connections, which means they stay in existence until the Garbage collector comes along to remove them. Which may be next week, or next month, or ...
And Connections are scarce resources - you will run out of them a lot sooner than you will run out of memory and trigger the GC. Use a using block on your connection and command objects to ensure they are Disposed when they go out of scope.

And that leads to the second problem: as a result of the first problem you have an active SqlDataReader open when you exit - so you may well get problems from that when you try to do the next update. Use a using block on the reader as well.

And the third is: why the heck are you using a DateReader to do an update? Use ExecuteNonQuery instead.

But the main thing is: why are you doing this in a timer anyway? Particularly when you try to update and read images, this can take considerable time, and you will be monopolising your database to a stupid degree if the tick period is at all frequent.
Only ever update your DB if something has changed, not in a timer!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900