Repeating the same value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krisssgopi
    New Member
    • Apr 2010
    • 39

    Repeating the same value

    Below code is working fine. but when i enter the next value it was showing the previous value only. Please help me in this regard.

    Code:
     Try
                con.Open()
                param.ParameterName = "@uname"
                param.Value = TextBox2.Text
                cmd = New SqlCommand("select * from customer where custname=@uname", con)
                cmd.Parameters.AddWithValue("@uname", TextBox2.Text)
                da = New SqlDataAdapter(cmd)
                da.Fill(ds)
                TextBox1.Text = ds.Tables(0).Rows(0).Item(0)
                TextBox2.Text = ds.Tables(0).Rows(0).Item(1)
                TextBox4.Text = ds.Tables(0).Rows(0).Item(3)
                TextBox5.Text = ds.Tables(0).Rows(0).Item(5)
                DateTimePicker1.Value = ds.Tables(0).Rows(0).Item(4)
                TextBox3.Text = ds.Tables(0).Rows(0).Item(2)
    
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
            con.Close()
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by krisssgopi
    Hi Team,

    Below code is working fine. but when i enter the next value it was showing the previous value only. Please help me in this regard.

    Code:
     Try
                con.Open()
                param.ParameterName = "@uname"
                param.Value = TextBox2.Text
                cmd = New SqlCommand("select * from customer where custname=@uname", con)
                cmd.Parameters.AddWithValue("@uname", TextBox2.Text)
                da = New SqlDataAdapter(cmd)
                da.Fill(ds)
                TextBox1.Text = ds.Tables(0).Rows(0).Item(0)
                TextBox2.Text = ds.Tables(0).Rows(0).Item(1)
                TextBox4.Text = ds.Tables(0).Rows(0).Item(3)
                TextBox5.Text = ds.Tables(0).Rows(0).Item(5)
                DateTimePicker1.Value = ds.Tables(0).Rows(0).Item(4)
                TextBox3.Text = ds.Tables(0).Rows(0).Item(2)
    
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
            con.Close()
    What's the next value? Where do you click to get it?...

    Explain in detail

    Comment

    • krisssgopi
      New Member
      • Apr 2010
      • 39

      #3
      Originally posted by ThatThatGuy
      What's the next value? Where do you click to get it?...

      Explain in detail
      Below snippet was done in button event. when i enter the value in text box it was showing the details of the first row of the table. I have sort out the reason why. since i have declare the row index value as 0. it was returning so. My question is when i enter another customer name it should return the values of the corresponding customer name detail. i can use loop for the same. but i cant able to do it. please advise me.


      Code:
      Try
                  con.Open()
                  param.ParameterName = "@uname"
                  param.Value = TextBox2.Text
                  cmd = New SqlCommand("select * from customer where custname=@uname", con)
                  cmd.Parameters.AddWithValue("@uname", TextBox2.Text)
                  da = New SqlDataAdapter(cmd)
                  da.Fill(ds)
                  TextBox1.Text = ds.Tables(0).Rows(0).Item(0)
                  TextBox2.Text = ds.Tables(0).Rows(0).Item(1)
                  TextBox4.Text = ds.Tables(0).Rows(0).Item(3)
                  TextBox5.Text = ds.Tables(0).Rows(0).Item(5)
                  DateTimePicker1.Value = ds.Tables(0).Rows(0).Item(4)
                  TextBox3.Text = ds.Tables(0).Rows(0).Item(2)
       
              Catch ex As Exception
                  MsgBox(ex.Message.ToString)
              End Try
              con.Close()

      Comment

      • medicinesoup
        New Member
        • May 2010
        • 5

        #4
        where is your param variable defined? For that matter, where are you declaring the con, cmd, da, and ds objects?

        I was able to duplicate the problem you are seeing. I believe it is related to the fact that your DataSet object doesn't go out of scope when the button_click event ends. SqlParameters and DataSets are a little wierd. They don't like being reused. Once you add the paramter as "Joe Customer" the first time, it works but it remembers those same values the second time you use it with a different customer name: "Jane Customer". It still has all the values from the first customer as well as the values from the second customer, but the first customer record is the one that is in the list first. Since the text boxes always populate from the first row of the dataset, the second customer information is never displayed.

        Also, I noticed you are setting the value of param, but you never use it. Instead the parameter value is being added to the command with the line:
        Code:
        cmd.Parameters.AddWithValue("@uname", TextBox2.Text)
        So you really don't need the param variable at all.

        I was able to get this to work splendidly once I declared the ds object locally. (Or you could set ds to a New instance of a DataSet. Either way works.)

        Here is the code after I added a line to reset the ds object to a new instance every time:
        Code:
                Try
                    con.Open()
                    'param.ParameterName = "@uname" NOT USED
                    'param.Value = TextBox2.Text NOT USED
                    cmd = New SqlCommand("select * from customer where custname=@uname", con)
                    cmd.Parameters.AddWithValue("@uname", TextBox2.Text)
                    da = New SqlDataAdapter(cmd)
                    ds = New DataSet 'Dataset must be reset by setting it to a new instance
                    da.Fill(ds)
                    TextBox1.Text = ds.Tables(0).Rows(0).Item(0)
                    TextBox2.Text = ds.Tables(0).Rows(0).Item(1)
                    TextBox4.Text = ds.Tables(0).Rows(0).Item(3)
                    TextBox5.Text = ds.Tables(0).Rows(0).Item(5)
                    DateTimePicker1.Value = ds.Tables(0).Rows(0).Item(4)
                    TextBox3.Text = ds.Tables(0).Rows(0).Item(2)
        
                Catch ex As Exception
                    MsgBox(ex.Message.ToString)
                End Try
                con.Close()

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I don't understand what you mean by "next value" or "previous value". You have posted code that simply queries a database and displays the firs result of this query into TextBoxes....th ere is no "next" or "previous" to this code.

          Comment

          Working...