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

hasrow and execute reader loop gets error in the value exist check DB

hi iam using this code for datagrid row storage while check data is exist or not in DB sql server 2008

Expand|Select|Wrap|Line Numbers
  1. cmd.Parameters["@pname"].Value = row.Cells["gpname"].Value;
  2. cmd.Parameters["@cstock"].Value = row.Cells["gcstock"].Value;
  3. cmd.Parameters["@ondate"].Value = DateTime.Now;
  4. check();
  5.  
after to debug in comes in check function first time hasrow function is true value check and get display as value exist then loop 2nd time comes sqldatareader comesdirectly out and has row function is false
Expand|Select|Wrap|Line Numbers
  1.  using (Conn = new SqlConnection(constr))
  2. {
  3.   var name = cmd.Parameters["@pname"].Value;
  4.   var date = cmd.Parameters["@ondate"].Value;
  5.   string dd = String.Format("{0:yyyy/MM/dd}", date);
  6.   string query = "select pname from stock1 where pname='"+name+"' and ondate='"+dd+"'";
  7.   cmd1 = new SqlCommand(query, Conn);
  8.   cmd1.CommandType = CommandType.Text;
  9.   try
  10.   {
  11.     Conn.Open();
  12.     SqlDataReader sdr = cmd1.ExecuteReader();
  13.  
  14.     if(sdr.HasRows)
  15.     {
  16.       while (sdr.Read())
  17.       {
  18.         string s1 = sdr.GetString(0);
  19.         if (s1 == name.ToString())
  20.         {
  21.           MessageBox.Show("value already exist");
  22.         }
  23.         else
  24.         {
  25.           cmd.ExecuteNonQuery();
  26.         }
  27.  
  28.       }
  29.     }
  30.   }

how to set hasrow get always execute reader in the program
Nov 8 '14 #1
1 1618
Frinavale
9,735 Expert Mod 8TB
I don't fully understand your question but I do see some strange stuff in your code.

You are setting values for parameters for your SQL command but you actually don't have any parameters in the command text itself


Based on your posted code, this:
Expand|Select|Wrap|Line Numbers
  1. string query = "select pname from stock1 where pname='"+name+"' and ondate='"+dd+"'"; 
Should probably be this:
Expand|Select|Wrap|Line Numbers
  1. string query = "select pname from stock1 where pname=@pname and ondate=@ondate"; 
Fixing the SQL command isn't the only problem I see though: the fact that your ondate is being set to Now will very likely result in nothing being returned because it is highly unlikely that the date in the table will match the current date time since the date in the table was saved sometime in the past.

Also, you do not need to execute a query in the middle of reading the data of a query.... you should probably check out this MSDN document about the SqlCommand.ExecuteNonQuery Method for more information on how to use this function.

Also, check out this MSDN document on the SqlCommand.Parameters Property for more information on how to use parameters.
Nov 10 '14 #2

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

Similar topics

1
by: David C. allen | last post by:
I have created a simple Client-side SOAP Extension for a webclass that I have. When I apply the extension attribute to the the calling function in the proxy class I get an error 'Value cannot be...
0
by: volumstein | last post by:
I have a make-table query in a FOR loop that outputs to 'table1.' 'table1' is then referenced by 'Form1.' here's the code: For j = 1 To rsTable.RecordCount SerialNumRef.Value =...
3
by: deko | last post by:
I have a logging routine that's supposed to silently log errors caught by error handler code on certain functions. The problem is sometimes stuff happens and the error handler can get caught in a...
6
by: Josh Close | last post by:
I'm having a problem with a value coming out of a loop. CREATE OR REPLACE FUNCTION funmessagespermintotal() RETURNS int8 AS ' DECLARE this_rServer record; this_rSum record; this_iSum...
2
by: XML newbie: Urgent pls help! | last post by:
Hi, I am getting the error: Value of type 'String' cannot be converted to '1-dimensional array of Long'. in the following line for TextBox2.Text field : ...
0
by: XML newbie: Urgent pls help! | last post by:
Hi, I am getting the error: Value of type 'String' cannot be converted to '1-dimensional array of Long'. in the following line for TextBox2.Text field : ...
1
by: thesonofdad | last post by:
Hi all, Fun problem for everyone over the weekend - :) OK so I have the following SQL query in Access..... SELECT x.SURVEY_DATE AS , z.ORIG_CONST_DATE, z.ORIG_EXP_NO FROM . AS x, . AS z; ...
6
by: iDesmet | last post by:
Good day! I was wondering if someone could show me the way with this little problem I have. I need to get the value/text of every checked SubItems in a listview so I can execute a Sub. The...
0
by: utpont | last post by:
error value cannot be null. parameter name: s I get this error when trying to get onto a website using vista ie 7 the website is a flash website but opens only with that error is there a setting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...

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.