473,408 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1.  Try
  2.             con.Open()
  3.             param.ParameterName = "@uname"
  4.             param.Value = TextBox2.Text
  5.             cmd = New SqlCommand("select * from customer where custname=@uname", con)
  6.             cmd.Parameters.AddWithValue("@uname", TextBox2.Text)
  7.             da = New SqlDataAdapter(cmd)
  8.             da.Fill(ds)
  9.             TextBox1.Text = ds.Tables(0).Rows(0).Item(0)
  10.             TextBox2.Text = ds.Tables(0).Rows(0).Item(1)
  11.             TextBox4.Text = ds.Tables(0).Rows(0).Item(3)
  12.             TextBox5.Text = ds.Tables(0).Rows(0).Item(5)
  13.             DateTimePicker1.Value = ds.Tables(0).Rows(0).Item(4)
  14.             TextBox3.Text = ds.Tables(0).Rows(0).Item(2)
  15.  
  16.         Catch ex As Exception
  17.             MsgBox(ex.Message.ToString)
  18.         End Try
  19.         con.Close()
May 23 '10 #1
4 1305
ThatThatGuy
449 Expert 256MB
@krisssgopi
What's the next value? Where do you click to get it?...

Explain in detail
May 24 '10 #2
@ThatThatGuy
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.


Expand|Select|Wrap|Line Numbers
  1. Try
  2.             con.Open()
  3.             param.ParameterName = "@uname"
  4.             param.Value = TextBox2.Text
  5.             cmd = New SqlCommand("select * from customer where custname=@uname", con)
  6.             cmd.Parameters.AddWithValue("@uname", TextBox2.Text)
  7.             da = New SqlDataAdapter(cmd)
  8.             da.Fill(ds)
  9.             TextBox1.Text = ds.Tables(0).Rows(0).Item(0)
  10.             TextBox2.Text = ds.Tables(0).Rows(0).Item(1)
  11.             TextBox4.Text = ds.Tables(0).Rows(0).Item(3)
  12.             TextBox5.Text = ds.Tables(0).Rows(0).Item(5)
  13.             DateTimePicker1.Value = ds.Tables(0).Rows(0).Item(4)
  14.             TextBox3.Text = ds.Tables(0).Rows(0).Item(2)
  15.  
  16.         Catch ex As Exception
  17.             MsgBox(ex.Message.ToString)
  18.         End Try
  19.         con.Close()
May 25 '10 #3
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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1.         Try
  2.             con.Open()
  3.             'param.ParameterName = "@uname" NOT USED
  4.             'param.Value = TextBox2.Text NOT USED
  5.             cmd = New SqlCommand("select * from customer where custname=@uname", con)
  6.             cmd.Parameters.AddWithValue("@uname", TextBox2.Text)
  7.             da = New SqlDataAdapter(cmd)
  8.             ds = New DataSet 'Dataset must be reset by setting it to a new instance
  9.             da.Fill(ds)
  10.             TextBox1.Text = ds.Tables(0).Rows(0).Item(0)
  11.             TextBox2.Text = ds.Tables(0).Rows(0).Item(1)
  12.             TextBox4.Text = ds.Tables(0).Rows(0).Item(3)
  13.             TextBox5.Text = ds.Tables(0).Rows(0).Item(5)
  14.             DateTimePicker1.Value = ds.Tables(0).Rows(0).Item(4)
  15.             TextBox3.Text = ds.Tables(0).Rows(0).Item(2)
  16.  
  17.         Catch ex As Exception
  18.             MsgBox(ex.Message.ToString)
  19.         End Try
  20.         con.Close()
May 25 '10 #4
Frinavale
9,735 Expert Mod 8TB
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....there is no "next" or "previous" to this code.
May 27 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Sebastian Boeck | last post by:
Hello, whats the most simple way to insert the same value into 2 colums of the same table? My table is looking like this: Spalte | Typ | Attribute...
9
by: allenj | last post by:
DB2 UDB 7.2 WSE Fixpak 9 Linux Red Hat 7.3 I have some library code (written in Java, if that matters) that processes maintenance screens that are presented to the end-users as forms in a...
13
by: William Payne | last post by:
Hello, I would like to know if EXIT_FAILURE/SUCCESS has the same value on all platforms? I am writing a few scripts (I know shell scripts are off-topic here) to test my C programs and they depend...
11
by: The Crow | last post by:
i have a arraylist. say it contains integer values. i want to be able to inform user, which indexes in the array contain same values. but there can be N different values, and M different indexes...
8
by: Peter | last post by:
Hi, there I have created an stored procedure using the DDL below for my MS Access Database and no error occurs. Also it can create an stored procedure if I changed the parameter from "" to...
2
mickyp
by: mickyp | last post by:
Hi There, I have an access database where I would like to set two fields to the same value. Both fields are in the same table called Sampletbl The one field is called Year and the other Years....
4
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
21
by: vlsidesign | last post by:
This syntax does not to work nl, nt, ns = 0; The only one that get's initialized is ns. nl and nt because they don't initialize seem to get some junk from memory. I have done these two...
7
by: john_smith_1221 | last post by:
Hello, I need to use the rand() function to generate a random value, I already know how to do it with srand(time(NULL)) and its "randomness" is sufficient for me, the problem is my code requires to...
4
by: coderdon | last post by:
hi, i have two combo box with same values when i select some value in one combobox the other combo box should not allow to select the same value can any one help me with the javascript to do...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.