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, - SqlConnection MyConnection = new SqlConnection(Mysqlconnectionstring);
-
MyConnection.Open();
-
SqlCommand Cmd = new SqlCommand();
-
Cmd.Connection = MyConnection;
-
Cmd.CommandType = CommandType.Text ;
-
Cmd.CommandText = "select * from database where Number = '+Partno.ToString ()'";
-
SqlDataAdapter DA = new SqlDataAdapter(Cmd);
-
DataSet DS = new DataSet();
-
DA.Fill(DS);
-
Partname.ToString() = DS.Tables[0].Rows[1][1].ToString();
-
//all other fileds.
-
when I am executing this script it is giving the following error. - System.IndexOutOfRangeException
-
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.
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
Thanks! It worked.
A small + can make a world of difference.
To avoid these kinds of mistakes and since string concatenation is always messy, I like to use String.Format. -
string select = String.Format("select * from database where number = {0}",Partno.ToString());
-
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: - Cmd.CommandText = "select * from database where Number = @Number";
-
Cmd.Parameters.AddWithValue("@Number", Partno);
Plus you don't have to cast the parameter values to a string.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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.
|
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);
|
by: Reginald Bal |
last post by:
Hello,
I created following tables: &
SizeID (number)--KeyField
WeightFm (number)
WeightTo (number)
Sample data =
|
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
| |
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...
|
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...
|
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...
|
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..
|
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,...
|
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,...
| |
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |