Aashish682 says:
public
static
List<Users> Addusers(
string
name,
string
email,
string
mobile,
string
password,
string
cityid)
{
List<Users> register =
new
List<Users>();
string
connstring = ConfigurationManager.ConnectionStrings[
"connstring"
].ConnectionString;
using
(SqlConnection connection =
new
SqlConnection(connstring))
{
using
(SqlCommand command =
new
SqlCommand(
"SP_01Registeruser"
, connection))
{
connection.Open();
DataTable dt =
new
DataTable();
command.Parameters.AddWithValue(
"@name"
, name);
command.Parameters.AddWithValue(
"@email"
, email);
command.Parameters.AddWithValue(
"@mobile"
, mobile);
command.Parameters.AddWithValue(
"@password"
, password);
command.Parameters.AddWithValue(
"@cityid"
, cityid);
SqlDataAdapter da =
new
SqlDataAdapter(command);
da.Fill(dt);
SqlDataReader reader = command.ExecuteReader();
while
(reader.Read())
{
register.Add(
new
Users { CityID = reader[
"cityid"
].ToString(), UserID = reader[
"userid"
].ToString() });
}
reader.Close();
}
}
return
register;
}
the above code which you have used for inserting the data is not correct because you are inserting the data and not returning any values so you need to use the code like below
public static void Addusers(Users users)
{
string connstring = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connstring))
{
using (SqlCommand command = new SqlCommand("SP_01Registeruser", connection))
{
connection.Open();
command.Parameters.AddWithValue("@name", users.Name);
command.Parameters.AddWithValue("@email", users.Email);
command.Parameters.AddWithValue("@mobile", users.Mobile);
command.Parameters.AddWithValue("@password", users.Password);
command.Parameters.AddWithValue("@cityid", users.CityID);
command.ExecuteNonQuery();
connection.Close();
}
}
}
and if you are returning something after inserting data then you need to do like below
public static void Addusers(Users users)
{
string connstring = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connstring))
{
using (SqlCommand command = new SqlCommand("SP_01Registeruser", connection))
{
connection.Open();
command.Parameters.AddWithValue("@name", users.Name);
command.Parameters.AddWithValue("@email", users.Email);
command.Parameters.AddWithValue("@mobile", users.Mobile);
command.Parameters.AddWithValue("@password", users.Password);
command.Parameters.AddWithValue("@cityid", users.CityID);
//If you are returning single value for eg. if you are returning UserID.
object returnedValue = command.ExecuteScalar();
//If you are returning multiple list of users then use like below.
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
List<Users> register = new List<Users>();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
register.Add(new Users { CityID = reader["cityid"].ToString(), UserID = reader["userid"].ToString() });
}
//use list of users where you want to use.
connection.Close();
}
}
}