public List <MyBlog> getAllBlog() //profile detail of user by name { DataSet ds = new DataSet(); try { SqlConnection con = new SqlConnection(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=Blog;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; string Query = cmd.CommandText = "select Id,blogger_name,blog_subject,blog_details,blog_date from Blog "; SqlDataAdapter sda = new SqlDataAdapter(Query, con); sda.Fill(ds); } catch (FaultException fex) { throw new FaultException <string>("Error: " + fex); } List <MyBlog> objList = new List <MyBlog>(); foreach (DataRow _dataRow in ds.Tables[0].Rows) { MyBlog obj = new MyBlog(); obj.id = Convert.ToInt32(_dataRow["Id"]); obj.blogger_name = Convert.ToString(_dataRow["blogger_name"]); obj.blog_subject = Convert.ToString(_dataRow["blog_subject"]); obj.blog_details = Convert.ToString(_dataRow["blog_details"]); obj.blog_date = Convert.ToString(_dataRow["blog_date"]); objList.Add(obj); } return(objList); }
public MyBlog GetBlogById(int id) { MyBlog s = new MyBlog(); try { SqlConnection con = new SqlConnection(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=Blog;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"); con.Open(); SqlCommand cmd = new SqlCommand(@"SELECT * FROM Blog WHERE id=@id", con); cmd.Parameters.AddWithValue("@id", id); cmd.CommandType = CommandType.Text; using (SqlDataReader rdr = cmd.ExecuteReader()) { if (rdr.HasRows) { rdr.Read(); // get the first row s.id = rdr.GetInt32(0); s.blogger_name = rdr.GetString(1); s.blog_subject = rdr.GetString(2); s.blog_details = rdr.GetString(3); s.blog_date = rdr.GetString(4); } } } catch (FaultException fex) { throw new FaultException <string>("Error: " + fex); } return(s); }