473,670 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieve a record on itemid

21 New Member
Hi all,

My requirement is to allow users to enter partno. in a text box.
There is a button called 'populate'. whenever the user enters a partno. and hits on populate button, the matching record from the database should show up on the screen. Each field in the databse got corresponding textboxes on the screen.
I have used the following code:
under Button_Click event,
Expand|Select|Wrap|Line Numbers
  1. SqlConnection MyConnection = new SqlConnection(Mysqlconnectionstring);
  2.             MyConnection.Open();
  3.             SqlCommand Cmd = new SqlCommand();
  4.             Cmd.Connection = MyConnection;
  5.             Cmd.CommandType = CommandType.Text ;
  6.             Cmd.CommandText = "select * from database where Number = '+Partno.ToString ()'";
  7.             SqlDataAdapter DA = new SqlDataAdapter(Cmd);
  8.             DataSet DS = new DataSet();
  9.             DA.Fill(DS);
  10.           Partname.ToString() = DS.Tables[0].Rows[1][1].ToString();
  11.          //all other fileds.
  12.  
when I am executing this script it is giving the following error.
Expand|Select|Wrap|Line Numbers
  1. System.IndexOutOfRangeException
  2. There is no row at position 1.
But, a record with that partno does exists in the database.
Am I using any thing wrong here? Please advise.
Oct 16 '08 #1
3 1334
Plater
7,872 Recognized Expert Expert
You get index out of range because your datatable is empty.
You are not builing your commandtext correctly:

Cmd.CommandText = "select * from database where Number = '+Partno.ToStri ng ()'";
Will produce the literal string:
select * from database where Number = '+Partno.ToStri ng ()'
Assuming Partno is a number, try doing this:
Cmd.CommandText = "select * from database where Number = "+Partno.ToStri ng()+"";
Which should produce something like:
select * from database where Number = 1234
Oct 16 '08 #2
cathy25
21 New Member
Thanks! It worked.
A small + can make a world of difference.
Oct 16 '08 #3
Curtis Rutland
3,256 Recognized Expert Specialist
To avoid these kinds of mistakes and since string concatenation is always messy, I like to use String.Format.

Expand|Select|Wrap|Line Numbers
  1. string select = String.Format("select * from database where number = {0}",Partno.ToString());
  2.  
String.Format will replace {#} with the parameters that follow it. Much cleaner than lots of concatenation.

Of course, when using SQL, you can just use Parameterized Queries, which are easier, cleaner, and more secure:
Expand|Select|Wrap|Line Numbers
  1. Cmd.CommandText = "select * from database where Number = @Number";
  2. Cmd.Parameters.AddWithValue("@Number", Partno);
Plus you don't have to cast the parameter values to a string.
Oct 16 '08 #4

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

Similar topics

1
6668
by: Geir Baardsen | last post by:
Hi! On frmItems I have a two listboxes. The first, lstAllCategories, is loaded with data from tblCategory when frmItems open. When user click lstAllCategories, the lstSelectedItems will show all records for that category. Now when user clicks chosen item in lstSelectedItems, textboxes in form will be filled and now user can edit the selected item.
2
2371
by: Geir Baardsen | last post by:
Hi! I wasn't very clear, I think... On frmItems I have a listbox, lstShowItems, that is being filled up in the forms onload event with all records from tblItems. Now when user navigate between records I did wish the cursor would jump to the actual ItemID and select it in the lstShowItems.
4
1387
by: ad | last post by:
I found an example in Starter kit. Is it so tedious to update a record. ---------------------------------------------------------------------------- ------------- SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings); SqlCommand myCommand = new SqlCommand("Portal_UpdateContact", myConnection);
2
2615
by: Reginald Bal | last post by:
Hello, I created following tables: & SizeID (number)--KeyField WeightFm (number) WeightTo (number) Sample data =
1
1997
by: phpmaet | last post by:
Hi I have viewed the record from mysql table. But I need to retrieve the next record id while reading the each an every record. Could someone explain how I retrieve the next record id in a table(Note. WHILE READING THE CURRENT RECORD)? Thanks
13
3424
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end of the form which is submit and cancel. After i have clicked submit, the information is stored directly into my corresponding database table. My problem here is i need to retrieve back the information submitted to display all the data that the...
10
2631
by: stevlee | last post by:
I have a table that contains log entries. The table has two columns, the first column is a timestamp and the second column contains the data I need to parse. What I have to do is seperate the three fields in the second column into three columns. Here is an example of two rows of data from the second column; (TA:606:Duplicate Entry For ItemId SerialId ModelNo ) (TA:606:Duplicate Entry For ItemId SerialId ModelNo ) I am not too...
9
8216
by: weirdguy | last post by:
Hello all, I almost complete my mini project - Stock Inventory: To track goods/products enter and exit from warehouse or simply known as Stock Transaction (IN/OUT). This also include a Inventory Catalog which basically show all the products in the warehouse, each product details including category, description and most importantly quantity on hand. As mentioned above, I almost complete the database except the most important Form - New...
7
10278
by: shalskedar | last post by:
In a query i want to retrieve the value in such a way that if the value for the current record for a given column is 0 then it should go to the next record & take the value. for ex-I 've column Payment received in the query.I want to retrieve the value in such a way that if the current record value for this column is 0 then it should go to the next record value..
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8592
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5686
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4213
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2802
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.