Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. what is wrong in this code

what is wrong in this code

Scheduled Pinned Locked Moved Database
csharpquestionannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mm310
    wrote on last edited by
    #1

    private void button1_Click(object sender, EventArgs e) { string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\db4.MDB"; string strSQL = "SELECT * FROM ta1"; OleDbConnection myConn = new OleDbConnection(strDSN); OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, myConn); DataSet dtSet = new DataSet(); string sqlQuery = "SELECT Name,Password FROM ta1 where Name = '" + textBox1.Text.ToString() + "' AND Password= '" + textBox2.Text.ToString() + "'"; myCmd.SelectCommand.CommandText = sqlQuery; int numberOfRowsFeched = myCmd.Fill(dtSet, "ta1"); if (numberOfRowsFeched > 0) { try { myCmd.UpdateCommand.CommandText = "UPDATE Users SET " + "Password = '" +textBox3.Text + "'" + " WHERE Name = '" + textBox1.Text + "'"; myCmd.UpdateCommand.Connection = myConn; myCmd.UpdateCommand.ExecuteNonQuery(); MessageBox.Show("Record updated Successfully"); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } catch (System.Data.OleDb.OleDbException exp) { myConn.Close(); MessageBox.Show(exp.Message); } } else { MessageBox.Show("wrong Name Or Password", "Password", MessageBoxButtons.OK, MessageBoxIcon.Information); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } message : http://www.cpestudents.net/upload/up/54.gif[^]

    A C 2 Replies Last reply
    0
    • M mm310

      private void button1_Click(object sender, EventArgs e) { string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\db4.MDB"; string strSQL = "SELECT * FROM ta1"; OleDbConnection myConn = new OleDbConnection(strDSN); OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, myConn); DataSet dtSet = new DataSet(); string sqlQuery = "SELECT Name,Password FROM ta1 where Name = '" + textBox1.Text.ToString() + "' AND Password= '" + textBox2.Text.ToString() + "'"; myCmd.SelectCommand.CommandText = sqlQuery; int numberOfRowsFeched = myCmd.Fill(dtSet, "ta1"); if (numberOfRowsFeched > 0) { try { myCmd.UpdateCommand.CommandText = "UPDATE Users SET " + "Password = '" +textBox3.Text + "'" + " WHERE Name = '" + textBox1.Text + "'"; myCmd.UpdateCommand.Connection = myConn; myCmd.UpdateCommand.ExecuteNonQuery(); MessageBox.Show("Record updated Successfully"); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } catch (System.Data.OleDb.OleDbException exp) { myConn.Close(); MessageBox.Show(exp.Message); } } else { MessageBox.Show("wrong Name Or Password", "Password", MessageBoxButtons.OK, MessageBoxIcon.Information); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } message : http://www.cpestudents.net/upload/up/54.gif[^]

      A Offline
      A Offline
      albCode
      wrote on last edited by
      #2

      this keywords SET " + "Password as sql command in result is SETPassword, you have to separate by space, just set space character before Password like this SET " + " Password _____________________ Proud to be Albanian _____________________

      1 Reply Last reply
      0
      • M mm310

        private void button1_Click(object sender, EventArgs e) { string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\db4.MDB"; string strSQL = "SELECT * FROM ta1"; OleDbConnection myConn = new OleDbConnection(strDSN); OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, myConn); DataSet dtSet = new DataSet(); string sqlQuery = "SELECT Name,Password FROM ta1 where Name = '" + textBox1.Text.ToString() + "' AND Password= '" + textBox2.Text.ToString() + "'"; myCmd.SelectCommand.CommandText = sqlQuery; int numberOfRowsFeched = myCmd.Fill(dtSet, "ta1"); if (numberOfRowsFeched > 0) { try { myCmd.UpdateCommand.CommandText = "UPDATE Users SET " + "Password = '" +textBox3.Text + "'" + " WHERE Name = '" + textBox1.Text + "'"; myCmd.UpdateCommand.Connection = myConn; myCmd.UpdateCommand.ExecuteNonQuery(); MessageBox.Show("Record updated Successfully"); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } catch (System.Data.OleDb.OleDbException exp) { myConn.Close(); MessageBox.Show(exp.Message); } } else { MessageBox.Show("wrong Name Or Password", "Password", MessageBoxButtons.OK, MessageBoxIcon.Information); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } message : http://www.cpestudents.net/upload/up/54.gif[^]

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        mm310 wrote:

        what is wrong in this code

        You have left it wide open to SQL Injection Attacks. While the other response you got will apparently fix your problem it still leaves your code open to attack. You should read: SQL Injection Attacks and Tips on How to Prevent Them[^] This will explain a better way to perform your queries that will solve your problem AND make your code safer - Especially for code dealing with a login. What you have here is unforgivable and any tutorial that you have read that suggests you build SQL this way should be recalled and the author lined up and shot! (in my opinion). No wonder Software Developers have a bad reputation when it comes to security if developers think that concatenating strings together to form a SQL query is acceptable on a day-to-day basis. String concatenation as a way to build a SQL statement should only be done after careful consideration that there is absolutely no other way to achieve the desired result. Sorry for my rant. It isn't your fault - You've obviously read the wrong tutorials and have picked up some extremely dangerous habits. ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups