locked
List of Record RRS feed

  • Question

  • User1979860870 posted

    Hi

       I have below 2 methods (RecordNavigation , DataRecords). How i can use these 2 methods  in GetAllLocation

    public bool RecordNavigation(string SrcSql, SqlParameter[] paramArr, SrcType SourceType = SrcType.SqlQuery)
            {
                SqlCommand comm = new SqlCommand();
    
                try
                {
                    comm.CommandText = SrcSql;
    
                    if (SourceType == SrcType.SqlQuery)
                        comm.CommandType = CommandType.Text;
                    else if (SourceType == SrcType.Table)
                        comm.CommandType = CommandType.TableDirect;
                    else if (SourceType == SrcType.StoredProcedure)
                        comm.CommandType = CommandType.StoredProcedure;
    
                    comm.Connection = Cnn;
    
                    foreach (SqlParameter param in paramArr)
                        comm.Parameters.Add(param);
    
                    comm.Connection.Open();
                    if (comm.Connection.State == ConnectionState.Open)
                    {
                        if (comm.ExecuteNonQuery() > 0)
                            return true;
                    }
                }
                catch (Exception ex)
                {
    
                }
                finally
                {
                    comm.Parameters.Clear();
                    comm.Connection.Close();
                }
                return false;
            }
            public DataSet DataRecords(string SrcSql, SrcType SourceType = SrcType.SqlQuery)
            {
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(SrcSql, Cnn);
                da.Fill(ds);
                return ds;
            }
    
    public IEnumerable<Location> GetAllLocation()
            {
                List<Location> lstLocation = new List<Location>();
                using (SqlConnection con = Cnn)
                {
                    SqlCommand cmd = new SqlCommand("sp_Location", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action", "L");
                    con.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
    
                    while (rdr.Read())
                    {
                        Location location = new Location();
                        location.Code = Convert.ToInt32(rdr["Code"]);
                        location.Name = rdr["Name"].ToString();
                        location.Street = rdr["Street"].ToString();
                        location.City = rdr["City"].ToString();
                        lstLocation.Add(location);
                    }
                    con.Close();
                }
                return lstLocation;
            }

    Thanks

    Friday, January 15, 2021 11:06 AM

All replies

  • User1312693872 posted

    Hi,jagjit saini

    jagjit saini

    How i can use these 2 methods  in GetAllLocation

    What do you mean about use these 2 methods in GetAllLocation?

    If you want to use these method, you can call them with their corresponding parameter, like:

    public IEnumerable<Location> GetAllLocation()
            {
                SqlParameter[] parameters =
                {
                    new SqlParameter("parm1", SqlDbType.VarChar)
                };
                bool bool1 = RecordNavigation("string", parameters, SrcType.SqlQuery);
                DataSet ds1 =DataRecords("string", SrcType.SqlQuery);
    
                List<Location> lstLocation = new List<Location>();
                //.....
                return lstLocation;
            }

    Edit: If you mean call these two methods from models while the GetAllLocation() method is in controller, then add static to these two methods

    and then call them like:

    model:
    public static DataSet DataRecords(string SrcSql, SrcType SourceType = SrcType.SqlQuery) { DataSet ds = new DataSet(); //... return ds; }
    controller:
    DataSet ds1 = ModelName.DataRecords("string", SrcType.SqlQuery);

    Best Regards,

    Jerry Cai

    Monday, January 18, 2021 7:19 AM