help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SanMiguel12@gmail.com

    #1

    help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

    Hi, I have a huge problem which drives crazy for a month... I am
    working on a database program and I am using .net2005 and Sql server
    2003. In a form I have a listbox and two texboxes and there's also in
    my database a table with three sells: "name", "e-mail" and "address".
    The listbox shows the "name" and I want when I choose a name, the two
    textboxes to show the other two sells ("name","addres s") that goes for
    the name i chose in the table. And guess what? I don't know how!!! I
    know I have something like "SELECT...FROM. ..WHERE...LIKE. .." but the
    best think I ever got is "false" in the two textboxes... Please tell
    how to do that and also in which listbox's event I have to do this?
    PLEASE HELP ME !!!! IT'S ARGENT !!! Thanx a lot =)

  • tommaso.gastaldi@uniroma1.it

    #2
    Re: help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

    On 31 Lug, 13:14, SanMigue...@gma il.com wrote:
    Hi, I have a huge problem which drives crazy for a month... I am
    working on a database program and I am using .net2005 and Sql server
    2003. In a form I have a listbox and two texboxes and there's also in
    my database a table with three sells: "name", "e-mail" and "address".
    The listbox shows the "name" and I want when I choose a name, the two
    textboxes to show the other two sells ("name","addres s") that goes for
    the name i chose in the table. And guess what? I don't know how!!! I
    know I have something like "SELECT...FROM. ..WHERE...LIKE. .." but the
    best think I ever got is "false" in the two textboxes... Please tell
    how to do that and also in which listbox's event I have to do this?
    PLEASE HELP ME !!!! IT'S ARGENT !!! Thanx a lot =)
    it may become Gold if you post some code and a better explanation :-)

    T

    Comment

    • SanMiguel12@gmail.com

      #3
      Re: help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

      look I am not very good at .net and sql (and I am trying something
      difficult? very clever of me =D) but I will do my best.
      this is the best thing I've managed to do and it's not working.

      Private Sub Customers_Load( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load

      CustomersTableA dapter1.Fill(_D ol_Ammad_E_Stor e_DataSet11.Cus tomers)
      End Sub

      Private Sub ListBox1_Displa yMemberChanged( ByVal sender As Object,
      ByVal e As System.EventArg s) Handles ListBox1.Displa yMemberChanged

      Using connection As New SqlClient.SqlCo nnection("Data
      Source=DRJAMES; Initial Catalog=Dol-Ammad(E-Store);Integrat ed
      Security=True;" )
      connection.Open ()
      Using command As New SqlClient.SqlCo mmand("SELECT [e-mail]
      >From Customers WHERE Name = '" & ListBox1.Select edItem & "'",
      connection)
      TextBox2.Text = command.Execute Scalar.ToString
      End Using
      End Using
      End Sub

      Now, I don't know what else to sent you and to be honest I don't have
      anything else done on this problem... If need something else tell me
      what and I'll try to sent it. Thanx for your interest.

      Comment

      • Armin Zingler

        #4
        Re: help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

        <SanMiguel12@gm ail.comschrieb
        Hi, I have a huge problem which drives crazy for a month... I am
        working on a database program and I am using .net2005 and Sql server
        2003. In a form I have a listbox and two texboxes and there's also
        in my database a table with three sells: "name", "e-mail" and
        "address". The listbox shows the "name" and I want when I choose a
        name, the two textboxes to show the other two sells
        ("name","addres s") that goes for the name i chose in the table. And
        guess what? I don't know how!!! I know I have something like
        "SELECT...FROM. ..WHERE...LIKE. .." but the best think I ever got is
        "false" in the two textboxes... Please tell how to do that and also
        in which listbox's event I have to do this? PLEASE HELP ME !!!! IT'S
        ARGENT !!! Thanx a lot =)

        Assuming that the record exists and the name is unique:

        'untested:
        Private Sub ListBox1_Select edIndexChanged( _
        ByVal sender As System.Object, ByVal e As System.EventArg s) _
        Handles ListBox1.Select edIndexChanged

        Dim name As String
        Dim cmd As SqlCommand
        Dim dr As SqlDataReader

        name = ListBox1.Select edItem.ToString

        cmd = New SqlCommand("sel ect * from persons where name = @name")
        cmd.Parameters. Add("@name", SqlDbType.NVarC har, 50).Value = name

        dr = cmd.ExecuteRead er
        dr.Read()
        txtAddress.Text = dr("address").T oString
        txtEMail.Text = dr("[e-mail]").ToString
        dr.Close()

        End Sub


        Though, is the name unique? If yes, why do you use LIKE, and if no, what if
        there are several records with the same name?


        Armin

        Comment

        • tommaso.gastaldi@uniroma1.it

          #5
          Re: help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

          On 31 Lug, 13:54, SanMigue...@gma il.com wrote:
          look I am not very good at .net and sql (and I am trying something
          difficult? very clever of me =D) but I will do my best.
          this is the best thing I've managed to do and it's not working.
          >
          Private Sub Customers_Load( ByVal sender As System.Object, ByVal e As
          System.EventArg s) Handles MyBase.Load
          >
          CustomersTableA dapter1.Fill(_D ol_Ammad_E_Stor e_DataSet11.Cus tomers)
          End Sub
          >
          Private Sub ListBox1_Displa yMemberChanged( ByVal sender As Object,
          ByVal e As System.EventArg s) Handles ListBox1.Displa yMemberChanged
          >
          Using connection As New SqlClient.SqlCo nnection("Data
          Source=DRJAMES; Initial Catalog=Dol-Ammad(E-Store);Integrat ed
          Security=True;" )
          connection.Open ()
          Using command As New SqlClient.SqlCo mmand("SELECT [e-mail]>From Customers WHERE Name = '" & ListBox1.Select edItem & "'",
          >
          connection)
          TextBox2.Text = command.Execute Scalar.ToString
          End Using
          End Using
          End Sub
          >
          Now, I don't know what else to sent you and to be honest I don't have
          anything else done on this problem... If need something else tell me
          what and I'll try to sent it. Thanx for your interest.
          try ExecuteReader and get the field values you need.

          ExecuteScalar returns 1 value and there is no way you can fill 2
          TexBoxes with 1 value.
          Further, that value may not be what you want, as for instance is a
          field of no interest to you.

          T

          Comment

          • SanMiguel12@gmail.com

            #6
            Re: help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

            Yes the name is unique and I was using LIKE in this way:

            SqlDataAdapter1 .SelectCommand. CommandText = "SELECT e-mail FROM
            persons WHERE name LIKE '" & ListBox1.Text & "'"
            TextBox2.Text = SqlDataAdapter1 .ToString

            I tried what you sent me but still nothing happend. =( there's no
            problem with the code, the program runs without faults and when I
            change the listbox item the two textboxes remain empty. WHY???????? I
            know that the solution is like the one you sent but it doesn't work.

            Comment

            • Armin Zingler

              #7
              Re: help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

              "Armin Zingler" <az.nospam@free net.deschrieb
              >
              cmd = New SqlCommand("sel ect * from persons where name = @name")
              Sry, forgot the connection:

              cmd = New SqlCommand("sel ect * from persons where name = @name", con)


              And too many brackets:
              txtEMail.Text = dr("[e-mail]").ToString
              txtEMail.Text = dr("e-mail").ToString


              Armin

              Comment

              • Armin Zingler

                #8
                Re: help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

                <SanMiguel12@gm ail.comschrieb
                Yes the name is unique and I was using LIKE in this way:
                >
                SqlDataAdapter1 .SelectCommand. CommandText = "SELECT e-mail FROM
                persons WHERE name LIKE '" & ListBox1.Text & "'"
                TextBox2.Text = SqlDataAdapter1 .ToString

                I'm a bit confused because, in the other posting

                a) you don't use LIKE
                b) the last line is

                TextBox2.Text = command.Execute Scalar.ToString

                c) you use Listbox1.Select edItem, not Listbox1.Text

                The latter shouldn't work if you enable Option Strict.



                Armin

                Comment

                • SanMiguel12@gmail.com

                  #9
                  Re: help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

                  Oh, sorry. My fault! Let's forget completely the LIKE thing and all
                  the stupid code I wrote... ( I told you that obviously I was writting
                  whatever I found because it's almost a month I am trying to fix this,
                  so everything is wrong!!!).
                  Now, the code I am using right now - and still not working... - is
                  this:

                  Private Sub ListBox1_Select edIndexChanged( ByVal sender As
                  System.Object, ByVal e As System.EventArg s)
                  Dim name As String
                  Dim cmd As SqlClient.SqlCo mmand
                  Dim dr As SqlClient.SqlDa taReader

                  name = ListBox1.Select edItem.ToString

                  cmd = New SqlClient.SqlCo mmand("SELECT * FROM Customers WHERE
                  name = @name", SqlConnection1)
                  cmd.Parameters. Add("@name", SqlDbType.NVarC har, 50).Value =
                  name

                  dr = cmd.ExecuteRead er
                  dr.Read()
                  TextBox3.Text = dr("address").T oString
                  TextBox2.Text = dr("E-mail").ToString
                  dr.Close()

                  End Sub

                  Do you find anything wrong???

                  Comment

                  • Armin Zingler

                    #10
                    Re: help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

                    <SanMiguel12@gm ail.comschrieb
                    Oh, sorry. My fault! Let's forget completely the LIKE thing and all
                    the stupid code I wrote... ( I told you that obviously I was
                    writting whatever I found because it's almost a month I am trying to
                    fix this, so everything is wrong!!!).
                    Now, the code I am using right now - and still not working... - is
                    this:
                    >
                    Private Sub ListBox1_Select edIndexChanged( ByVal sender As
                    System.Object, ByVal e As System.EventArg s)
                    Dim name As String
                    Dim cmd As SqlClient.SqlCo mmand
                    Dim dr As SqlClient.SqlDa taReader
                    >
                    name = ListBox1.Select edItem.ToString
                    >
                    cmd = New SqlClient.SqlCo mmand("SELECT * FROM Customers WHERE
                    name = @name", SqlConnection1)
                    cmd.Parameters. Add("@name", SqlDbType.NVarC har, 50).Value =
                    name
                    >
                    dr = cmd.ExecuteRead er
                    dr.Read()
                    TextBox3.Text = dr("address").T oString
                    TextBox2.Text = dr("E-mail").ToString
                    dr.Close()
                    >
                    End Sub
                    >
                    Do you find anything wrong???
                    No, nothing wrong.

                    Are you sure you connected to the right database? Right table? Really no
                    duplicate names?
                    I guess you have checked this alredy, but...


                    Is it possible that you can send me the project via e-mail?
                    Use the sender address of this post and insert a "_" between "no" and
                    "spam".


                    Armin

                    Comment

                    • zacks@construction-imaging.com

                      #11
                      Re: help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

                      On Jul 31, 8:50 am, SanMigue...@gma il.com wrote:
                      Oh, sorry. My fault! Let's forget completely the LIKE thing and all
                      the stupid code I wrote... ( I told you that obviously I was writting
                      whatever I found because it's almost a month I am trying to fix this,
                      so everything is wrong!!!).
                      Now, the code I am using right now - and still not working... - is
                      this:
                      >
                      Private Sub ListBox1_Select edIndexChanged( ByVal sender As
                      System.Object, ByVal e As System.EventArg s)
                      Dim name As String
                      Dim cmd As SqlClient.SqlCo mmand
                      Dim dr As SqlClient.SqlDa taReader
                      >
                      name = ListBox1.Select edItem.ToString
                      >
                      cmd = New SqlClient.SqlCo mmand("SELECT * FROM Customers WHERE
                      name = @name", SqlConnection1)
                      cmd.Parameters. Add("@name", SqlDbType.NVarC har, 50).Value =
                      name
                      >
                      dr = cmd.ExecuteRead er
                      dr.Read()
                      TextBox3.Text = dr("address").T oString
                      TextBox2.Text = dr("E-mail").ToString
                      dr.Close()
                      >
                      End Sub
                      >
                      Do you find anything wrong???
                      Try running your code with the SQL Profiler running a trace. Verify
                      that the SQL statement being executed is what you expect it to be. You
                      are still using the .SelectedItem property as opposed to the.Text
                      property, that may be part of the problem. Perhaps single step through
                      the code and after the variable "name" is assigned a value, dump the
                      value in the command window and verify the variable was assigned the
                      correct value.


                      Comment

                      Working...