[WebMethod]
public static string NewUsers(Users user)
{
string config = Convert.ToString(ConfigurationManager.ConnectionStrings["myconnection"]);
List<Users> Users = new List<Users>();
SqlConnection con = new SqlConnection(config);
con.Open();
using (SqlCommand cmd = new SqlCommand("insert into users values(@Name,@Role)", con))
{
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = user.Name;
cmd.Parameters.Add("@Role", SqlDbType.VarChar).Value = user.role;
cmd.ExecuteNonQuery();
}
con.Close();
return JsonConvert.SerializeObject(new { status = true, });
}
/// <summary>
/// this function get all users data.
/// </summary>
/// <returns></returns>
[WebMethod]
public static string getData()
{
string config = Convert.ToString(ConfigurationManager.ConnectionStrings["myconnection"]);
List<Users> Users = new List<Users>();
SqlConnection con = new SqlConnection(config);
con.Open();
using (SqlCommand cmd = new SqlCommand("Select * from Users", con))
{
SqlDataReader idr = cmd.ExecuteReader();
if (idr.HasRows)
{
Users = populateLisst(idr, con);
}
}
con.Close();
return JsonConvert.SerializeObject(Users);
}
public static List<Users> populateLisst(SqlDataReader idr, SqlConnection con)
{
List<Users> usersI = new List<Users>();
while (idr.Read())
{
usersI.Add(new Users
{
id = Convert.ToInt32(idr["userId"]),
Name = Convert.ToString(idr["name"]),
role = Convert.ToString(idr["role"])
});
}
return usersI;
}
public class Users
{
public int id { get; set; }
public string Name { get; set; }
public string role { get; set; }
}