473,320 Members | 1,876 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,320 software developers and data experts.

Retrieve a record on itemid

21
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 1320
Plater
7,872 Expert 4TB
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.ToString ()'";
Will produce the literal string:
select * from database where Number = '+Partno.ToString ()'
Assuming Partno is a number, try doing this:
Cmd.CommandText = "select * from database where Number = "+Partno.ToString()+"";
Which should produce something like:
select * from database where Number = 1234
Oct 16 '08 #2
cathy25
21
Thanks! It worked.
A small + can make a world of difference.
Oct 16 '08 #3
Curtis Rutland
3,256 Expert 2GB
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
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...
2
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...
4
by: ad | last post by:
I found an example in Starter kit. Is it so tedious to update a record. ---------------------------------------------------------------------------- ------------- SqlConnection myConnection...
2
by: Reginald Bal | last post by:
Hello, I created following tables: & SizeID (number)--KeyField WeightFm (number) WeightTo (number) Sample data =
1
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...
13
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...
10
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...
9
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...
7
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.