By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,154 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,154 IT Pros & Developers. It's quick & easy.

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

P: 1
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();
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();
  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.         }
  28.       }
  29.     }
  30.   }

how to set hasrow get always execute reader in the program
Nov 8 '14 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,731
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

Post your reply

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