Click here to Skip to main content
16,004,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my project I am struck up at the following code. I am passing 2 parameter values to check a record in a table. It gives problem.

C#
SqlCommand cmd = new SqlCommand("select * from progressive where mem_id= '" + v_mem_id + "' and dist_code='" + v_dist_code + "'", con);
SqlParameter param = new SqlParameter();
param.ParameterName = "@v_mem_id";
cmd.Parameters.Add(param);
SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@v_dist_code";
cmd.Parameters.Add(param1);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{

}
else
{
}


Can someone help me out.
Posted
Updated 15-Apr-13 19:21pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Apr-13 0:45am    
"It give problem" is not informative.
—SA
PrashantSonewane 16-Apr-13 0:47am    
you are aleady adding the values in SQLCommand. You dont need to pass any parameter values. Remove the code of adding param and param1. It should work..

C#
SqlCommand cmd = new SqlCommand("select * from progressive where mem_id= '" + v_mem_id + "' and dist_code='" + v_dist_code + "'", con);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {

            }
            else
            {
            }



And i believe you have some values in v_mem_id and v_dist_code. As i said in comment, you dont need to pass any param as your commandstring contains the values already.
 
Share this answer
 
SqlCommand cmd = new SqlCommand("select * from progressive where mem_id= @v_mem_id and dist_code=@v_dist_code", con);
cmd.Parameters.AddWithValue("@v_mem_id", v_mem_id);
cmd.Parameters.AddWithValue("@v_dist_code", v_dist_code);

connection.Open() // your SqlConnection
SqlDataReader dr = cmd.ExecuteReader();

while(dr.read())
{
// Your Object filling with data use dr.getValue(columnPosition) or dr["ColumnName"].ToString()
// dr.getString(columnPosition) or dr.getInt(columnPosition) according your data type
}

connection.Close() // close SqlConnection

// thats all, keep your code in try catch :)
 
Share this answer
 

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