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. General Programming
  3. C#
  4. Error in retrieving data from SQL

Error in retrieving data from SQL

Scheduled Pinned Locked Moved C#
csharplearningasp-netdatabasesql-server
8 Posts 4 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
    misCafe
    wrote on last edited by
    #1

    Dear brothers, I have read the book and searched from the internet. They told me the same way in retrieving data from SQL server. Unfortunately, it errors something when I do the same way. See my codes below : private void btn_login_Click(object sender, EventArgs e) { string sSQL = "SELECT USERNAME, USERPASSWORD FROM TBL_USER WHERE USERNAME = '" + this.txt_username.Text + "' AND USERPASSWORD = '" + this.txt_username.Text + "'"; SqlCommand sCommand = new SqlCommand(sSQL, dbConnection.sqlCnn); try { dbConnection.sqlCnn.Open(); SqlDataAdapter sReader = sCommand.ExecuteReader(); // It says : "Cannot implicitly convert type'System.Data.SqlClient.SqlDataReader' to 'System.Data.SqlClient.SqlDataAdapter'" while (sReader.Read() == true) { frm_main frmparents = new frm_main(); frmparents.ShowDialog(); } } catch (Exception Er) { MessageBox.Show(Er.Message, "Exception"); } Thanks for reading the entire article.

    Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

    L D 2 Replies Last reply
    0
    • M misCafe

      Dear brothers, I have read the book and searched from the internet. They told me the same way in retrieving data from SQL server. Unfortunately, it errors something when I do the same way. See my codes below : private void btn_login_Click(object sender, EventArgs e) { string sSQL = "SELECT USERNAME, USERPASSWORD FROM TBL_USER WHERE USERNAME = '" + this.txt_username.Text + "' AND USERPASSWORD = '" + this.txt_username.Text + "'"; SqlCommand sCommand = new SqlCommand(sSQL, dbConnection.sqlCnn); try { dbConnection.sqlCnn.Open(); SqlDataAdapter sReader = sCommand.ExecuteReader(); // It says : "Cannot implicitly convert type'System.Data.SqlClient.SqlDataReader' to 'System.Data.SqlClient.SqlDataAdapter'" while (sReader.Read() == true) { frm_main frmparents = new frm_main(); frmparents.ShowDialog(); } } catch (Exception Er) { MessageBox.Show(Er.Message, "Exception"); } Thanks for reading the entire article.

      Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      misCafe wrote:

      AND USERPASSWORD = '" + this.txt_username.Text + "'";

      :confused::confused::confused: BTW: normally one does not store the password, instead it gets hashed and it is the hash that gets stored and compared/searched.

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      M 1 Reply Last reply
      0
      • L Luc Pattyn

        misCafe wrote:

        AND USERPASSWORD = '" + this.txt_username.Text + "'";

        :confused::confused::confused: BTW: normally one does not store the password, instead it gets hashed and it is the hash that gets stored and compared/searched.

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        M Offline
        M Offline
        misCafe
        wrote on last edited by
        #3

        Oh yes, I have changed the text box. But it still errors when I press F5. Then how?

        Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

        L 1 Reply Last reply
        0
        • M misCafe

          Oh yes, I have changed the text box. But it still errors when I press F5. Then how?

          Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, I have no experience with SqlDataAdapter, however the compiler is correct (obviously) when it says the result of ExecuteReader() is an SqlDataReader and not an SqlDataAdapter. I suggest you look at the relevant documentation and/or google for correct examples. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          1 Reply Last reply
          0
          • M misCafe

            Dear brothers, I have read the book and searched from the internet. They told me the same way in retrieving data from SQL server. Unfortunately, it errors something when I do the same way. See my codes below : private void btn_login_Click(object sender, EventArgs e) { string sSQL = "SELECT USERNAME, USERPASSWORD FROM TBL_USER WHERE USERNAME = '" + this.txt_username.Text + "' AND USERPASSWORD = '" + this.txt_username.Text + "'"; SqlCommand sCommand = new SqlCommand(sSQL, dbConnection.sqlCnn); try { dbConnection.sqlCnn.Open(); SqlDataAdapter sReader = sCommand.ExecuteReader(); // It says : "Cannot implicitly convert type'System.Data.SqlClient.SqlDataReader' to 'System.Data.SqlClient.SqlDataAdapter'" while (sReader.Read() == true) { frm_main frmparents = new frm_main(); frmparents.ShowDialog(); } } catch (Exception Er) { MessageBox.Show(Er.Message, "Exception"); } Thanks for reading the entire article.

            Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #5

            misCafe wrote:

            SqlDataAdapter sReader = sCommand.ExecuteReader();

            This is where the problem is. ExecuteReader method returns a DataReader object and NOT the DataAdapter object. In case you need to use the DataAdapter, initialize it using the parameterized constructor and then use its Fill method. ObjectBrowser will help you finding about these.

            जय हिंद

            M 2 Replies Last reply
            0
            • D dan sh

              misCafe wrote:

              SqlDataAdapter sReader = sCommand.ExecuteReader();

              This is where the problem is. ExecuteReader method returns a DataReader object and NOT the DataAdapter object. In case you need to use the DataAdapter, initialize it using the parameterized constructor and then use its Fill method. ObjectBrowser will help you finding about these.

              जय हिंद

              M Offline
              M Offline
              misCafe
              wrote on last edited by
              #6

              Many thanks for taking a part on this article ;)

              Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

              M 1 Reply Last reply
              0
              • M misCafe

                Many thanks for taking a part on this article ;)

                Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

                M Offline
                M Offline
                MumbleB
                wrote on last edited by
                #7

                Also, please check Readup on SQL Injection.

                Excellence is doing ordinary things extraordinarily well.

                1 Reply Last reply
                0
                • D dan sh

                  misCafe wrote:

                  SqlDataAdapter sReader = sCommand.ExecuteReader();

                  This is where the problem is. ExecuteReader method returns a DataReader object and NOT the DataAdapter object. In case you need to use the DataAdapter, initialize it using the parameterized constructor and then use its Fill method. ObjectBrowser will help you finding about these.

                  जय हिंद

                  M Offline
                  M Offline
                  misCafe
                  wrote on last edited by
                  #8

                  Oh well, It was my mistake. I may blind! It must be SqlDataReader in fact! Thanks again!

                  Chuon Visoth Angkor Wat - Cambodia asp.net - c sharp beginner

                  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