Click here to Skip to main content
15,884,783 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi iam using this code for row storage while check data is exist or not in DB sql server 2008

cmd.Parameters["@pname"].Value = row.Cells["gpname"].Value;
                            cmd.Parameters["@cstock"].Value = row.Cells["gcstock"].Value;
                            cmd.Parameters["@ondate"].Value = DateTime.Now;
                            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 comes directly out of function and row function is false

using (Conn = new SqlConnection(constr))
            {
                var name = cmd.Parameters["@pname"].Value;
                var date = cmd.Parameters["@ondate"].Value;
                string dd = String.Format("{0:yyyy/MM/dd}", date);
                string query = "select pname from stock1 where pname='"+name+"' and ondate='"+dd+"'";
                cmd1 = new SqlCommand(query, Conn);
                cmd1.CommandType = CommandType.Text;
                try
                {
                    Conn.Open();
                SqlDataReader sdr = cmd1.ExecuteReader();
               
                    if(sdr.HasRows)
                    {


                        while (sdr.Read())
                        {
                            string s1 = sdr.GetString(0);
                            if (s1 == name.ToString())
                            {
                                MessageBox.Show("value already exist");

                            }
                            else
                            {
                                cmd.ExecuteNonQuery();
                            }

                        }
                    }
                }



how to set hasrow get always true execute reader in the program
Posted
Comments
Tomas Takac 8-Nov-14 6:13am    
I don't understand. You are calling check twice? Where? Or you mean looping trough while (sdr.Read()) two times? Setting aside the fact you should be doing this differently.

1 solution

I would do as below
C#
using(SqlConnection conn = new SqlConnection(constr))
using(SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM stock1 WHERE pname =@pname and ondate =@ondate", conn))
{
    comm.Parameters.AddWithValue("@pname", name);
    comm.Parameters.AddWithValue("@ondate", date);
    conn.Open();
    int count = (int) comm .ExecuteScalar();
    if(count>0)
        MessageBox.Show("value already exist");
}


You have Check method, update the method to pass name and ondate as method parameters.
nested database queries makes difficult to find the issues and while using readers and commands with same sql connection. You better refactor your code and try to apply below best practices.

-Don't share the connection objects, create one and use it, after you can properly dispose the connection. (wrap the connection, command etc with using blocks, it will automatically dispose the object)
- use parameters, it is safe and you are not open for sql injection attacks
- you can use ExecuteScalar method in this case to check whether row exist with given values. ( learn about methods available with which methods most suitable for your requirements)
 
Share this answer
 
v2
Comments
Manas Bhardwaj 8-Nov-14 7:40am    
Agree +5
DamithSL 8-Nov-14 7:54am    
Thank you Manas

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900