hi friends fetching datetime values from database, when data is null in code side it is displying the some garbage value instead that i want to show 'date is not found' the result is showind in grid view control

service written for fetching datetime

public List<Topics> GetAllTopicNames(int communityId)
    {
        List<Topics> communityTopiclist = new List<Topics>();

        try
        {
            string connectionString = ConfigurationSettings.AppSettings["connectionString"];
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * from (select cd.Community_id,ct.Topic_id ,ct.Name,ct.Posts from Community_Details cd,Community_Topic_Details ct where ct.Community_Id = cd.Community_Id ) as C left join (select cr.topic_id , max(cr.reply_date) as RecentPost from Community_Topic_Details ctr,Community_Replys cr where ctr.topic_id=cr.topic_id group by cr.topic_id) as rep on rep .topic_Id=c.Topic_Id where C.Community_id=" + communityId.ToString(), con);
            
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows != false)
            {
                while (dr.Read())
                {
                    Topics communityTopics = new Topics();
                    if (dr["Topic_Id"] != System.DBNull.Value)      
                    communityTopics.TopicId = int.Parse(dr["Topic_Id"].ToString());
                    if (dr["Name"] != System.DBNull.Value)      
                    communityTopics.TopicName = dr["Name"].ToString();
                    if (dr["Posts"] != System.DBNull.Value)      
                    communityTopics.TopicPosts = int.Parse(dr["Posts"].ToString());                                
                    if (dr["RecentPost"] != System.DBNull.Value)
                    communityTopics.LastPost = DateTime.Parse(dr["RecentPost"].ToString());
                    
                   
                   communityTopiclist.Add(communityTopics);

                }
            }
            else
            {
                return null;
            }

        }
        catch (Exception ex)
        {
            return null;
        }
        return communityTopiclist;


    }



  public void GetAllTopics()
    {
        CommunityManagement.CommunityManagementSoap topicService = new CommunityManagement.CommunityManagementSoapClient();
        GetAllTopicNamesRequest topicRequest = new GetAllTopicNamesRequest();
        topicRequest.Body = new GetAllTopicNamesRequestBody();
        topicRequest.Body.communityId = int.Parse(Request.QueryString["communityId"]);
        GetAllTopicNamesResponse topicResponse = topicService.GetAllTopicNames(topicRequest);
        if (topicResponse != null && topicResponse.Body != null && topicResponse.Body.GetAllTopicNamesResult != null)
        {
            
                GridViewCommunityTopics.DataSource = topicResponse.Body.GetAllTopicNamesResult;
                GridViewCommunityTopics.DataBind();
                PanelMiddle.Controls.Add(GridViewCommunityTopics);

                //.Controls.Add(GridViewCommunityTopics);
            
        }
        else
            GridViewCommunityTopics.DataSource = null;

    }

Recommended Answers

All 2 Replies

Is this ASP.NET or GUI question? (Just so I can move this post to more relevant forum section. MS SQL section is more of queries questions and DB maintenance)

hi friend ,
use following query in your code(sql command)

select case when date1 is null then 'date is not found' else convert(nvarchar,date1,105) end as date1 from datetest

regards,
rathnakar

hi friends fetching datetime values from database, when data is null in code side it is displying the some garbage value instead that i want to show 'date is not found' the result is showind in grid view control

service written for fetching datetime

public List<Topics> GetAllTopicNames(int communityId)
    {
        List<Topics> communityTopiclist = new List<Topics>();

        try
        {
            string connectionString = ConfigurationSettings.AppSettings["connectionString"];
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * from (select cd.Community_id,ct.Topic_id ,ct.Name,ct.Posts from Community_Details cd,Community_Topic_Details ct where ct.Community_Id = cd.Community_Id ) as C left join (select cr.topic_id , max(cr.reply_date) as RecentPost from Community_Topic_Details ctr,Community_Replys cr where ctr.topic_id=cr.topic_id group by cr.topic_id) as rep on rep .topic_Id=c.Topic_Id where C.Community_id=" + communityId.ToString(), con);
            
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows != false)
            {
                while (dr.Read())
                {
                    Topics communityTopics = new Topics();
                    if (dr["Topic_Id"] != System.DBNull.Value)      
                    communityTopics.TopicId = int.Parse(dr["Topic_Id"].ToString());
                    if (dr["Name"] != System.DBNull.Value)      
                    communityTopics.TopicName = dr["Name"].ToString();
                    if (dr["Posts"] != System.DBNull.Value)      
                    communityTopics.TopicPosts = int.Parse(dr["Posts"].ToString());                                
                    if (dr["RecentPost"] != System.DBNull.Value)
                    communityTopics.LastPost = DateTime.Parse(dr["RecentPost"].ToString());
                    
                   
                   communityTopiclist.Add(communityTopics);

                }
            }
            else
            {
                return null;
            }

        }
        catch (Exception ex)
        {
            return null;
        }
        return communityTopiclist;


    }



  public void GetAllTopics()
    {
        CommunityManagement.CommunityManagementSoap topicService = new CommunityManagement.CommunityManagementSoapClient();
        GetAllTopicNamesRequest topicRequest = new GetAllTopicNamesRequest();
        topicRequest.Body = new GetAllTopicNamesRequestBody();
        topicRequest.Body.communityId = int.Parse(Request.QueryString["communityId"]);
        GetAllTopicNamesResponse topicResponse = topicService.GetAllTopicNames(topicRequest);
        if (topicResponse != null && topicResponse.Body != null && topicResponse.Body.GetAllTopicNamesResult != null)
        {
            
                GridViewCommunityTopics.DataSource = topicResponse.Body.GetAllTopicNamesResult;
                GridViewCommunityTopics.DataBind();
                PanelMiddle.Controls.Add(GridViewCommunityTopics);

                //.Controls.Add(GridViewCommunityTopics);
            
        }
        else
            GridViewCommunityTopics.DataSource = null;

    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.