473,508 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error:Index and length must refer to a location within the string.

10 New Member
Hi,
what is rong with my code?

Expand|Select|Wrap|Line Numbers
  1. int i = int.Parse(txtpret.Text);
  2.                 string query1 = "UPDATE Movie set pret = pret + '" + i + "'";
  3.                 string query2 = "UPDATE Movie set pret = pret - '" + i + "'";
  4.  
  5.                 MySqlCommand cmd1 = new MySqlCommand(query1, connection);
  6.                 MySqlCommand cmd2 = new MySqlCommand(query2, connection);
  7.  
  8.  
  9.                 //Execute command
  10.                 if (rbmareste.Checked)
  11.                     cmd1.ExecuteNonQuery();
  12.                 if (rbscade.Checked)
  13.                     cmd2.ExecuteNonQuery();  //here apears the error; cmd1 works fine;
  14.  
May 20 '10 #1
3 3566
nukefusion
221 Recognized Expert New Member
Judging by the fact that you convert the contents of txtPret to an Integer I'm guessing it's supposed to be a number, however, in your SQL statement your manipulating it as if it was a string. That's your error. Your code should read:

Expand|Select|Wrap|Line Numbers
  1. string query1 = "UPDATE Movie set pret = pret + " + i; 
  2. string query2 = "UPDATE Movie set pret = pret - " + i; 
  3.  
The first query passed because in effect it performs a concatenation. If pret=1 and i=1, after the update pret=11.

Hope this helps.
May 21 '10 #2
virtuoso
10 New Member
Thanks for the answer;
I manage to solve the problme. Now my ode looks like this:

Expand|Select|Wrap|Line Numbers
  1. # string query1 = "UPDATE Movie set pret = pret + '" + i + "'";
  2. # string query2 = "UPDATE Movie set pret = pret - " + i + "";
  3.  
form query2 i delete the ' ' but query1 looks just as before and works fine; I's strange but it's work;
May 21 '10 #3
nukefusion
221 Recognized Expert New Member
@virtuoso
Glad you have it working.

I'd double check your data because the first command is still treating i as a string while your second one is now treating it as an integer.

If your data's okay it may be because your SQL implementation is implicitly casting it back to an integer for you. If that's the case, although it works, it's unnecessary overhead and inconsistent. You should still consider modifying the first statement to treat i as an integer; it is after all, a number.
May 21 '10 #4

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

Similar topics

0
6473
by: Onder Kaya | last post by:
We have Windows NT 4.0 Oracle 8.1.7 and ORACLE ERP 11.5.8 We created new discrete job. This discrete job contains material requirements and we have custom form which is haz.issue. This form...
5
128944
by: Wade G. Pemberton | last post by:
Can't find it quickly in the reference books.
1
2302
by: Joe | last post by:
This is a weird problem. when doing trying to add a tabpage to a tabcontrol I get an error: Index was outside the bounds of the array. Now this only happens when I call a function from within...
3
29201
by: Jimski | last post by:
Hello all, I am having a problem where I get an error message when I call FlushFinalBlock when decrypting my encrypted text. I am using the Rijndael algorithm. The error message is "Length...
13
3629
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
2
6999
by: pnsreee | last post by:
Hi All, I have the following code. The array @sub_data will contain integers or a string"NO". I have to validate if the array contain integer. If it contain "NO" then no need to validate. ...
2
1736
by: Adam Nielsen | last post by:
Hi everyone, I've come up against an issue with template types. Interestingly enough it seems to trigger a bug in GCC 3.3.4 (it accepts the code but wrongly replaces the typename), whereas...
1
1507
by: toyin | last post by:
hello, pls can anyone tell me how to get around this error: operator + is not defined for string and system.data.datatable here is the code dim comm AS OdbcCommand ("insert into table2 select *...
1
3899
by: piyushgpt1 | last post by:
My company has a web application running on JBoss-2.4.4_Tomcat-4.0.1. It works fine for a few days, but once or twice in a week, getting the exception :- Error : Runnable did not complete within...
1
2992
by: =?Utf-8?B?SkI=?= | last post by:
Hello As I debug the C# code with a break point and by pressing F11 I eventually get a message stating: ContextSwitchDeadlock was detected Message: The CLR has been unable to transition from...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7333
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
7502
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
5637
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,...
1
5057
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...
0
3208
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...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
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 ...
0
428
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...

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.