5
Answers

MySqlcommand parameters in asp.net

HI,

I have created StoredProcedure in Mysql - 


CREATE PROCEDURE `SP_ValidateUser`(_action varchar(10), _userid varchar(150), _password varchar(100))
BEGIN
if _action = 'Validate' then
Select UserId, Password from table
where UserId = _userid and Password = _password;
end  if;
END



and for calling SP i want to create a common Function so I have created like - 


public DataTable GetCommonData(string SP, string action, string id, string str1, string str2)
    {
        DataTable dt = new DataTable();
        strcon.Open();
        MySqlCommand cmd = new MySqlCommand();
            MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
            cmd.Connection = strcon;
            cmd.CommandText = SP;
            cmd.CommandType = CommandType.StoredProcedure;
            string[] strcol = {action,id,str1,str2};
            foreach (string str  in strcol)
            {
                cmd.Parameters.AddWithValue("_action", str);
            }            
            ad.Fill(dt);
            ad.Dispose();
        return dt;
    }



for first value that is "action", it is taking value but while coming on second value it is showing error like - 


cmd.Parameters.AddWithValue("_action", str);


"_action" already used ...
i can define saperate this line for each value but
i don't want to use many lines...
is there any issue in my code?
Please let me know.....
Answers (5)