Results 1 to 6 of 6

Thread: Datagrid

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Malaysia
    Posts
    345

    Datagrid

    Hi!

    I have to search my database and display the search results in the datagrid.
    Its happening fine. I have created a subroutine BindSearchData() and calling it from the Click event of a button. The routine takes the query as an argument. Follwing is the code

    Code:
    private void BindSearchData(string strSearchSql)
    	{
    
              OleDbConnection scnnNW = new OleDbConnection(SQL_CONNECTION_STRING);
    	
    	  OleDbCommand scmd = new OleDbCommand(strSearchSql, scnnNW);
    			
    	  OleDbDataAdapter sda = new OleDbDataAdapter(scmd);
    
    	  DataSet ds = new DataSet();
    
    	  sda.Fill(ds);
    
    	  dgCompany.DataSource = ds;
    	  dgCompany.DataBind();
    
            }
    My problem is that when no records are found. I have to show it as a message " No Records found." I am coding in c# and a newbie.

    Pls help.
    Thanks.

  2. #2
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Datagrid

    not sure though, but you can do this by checking the dataset if fills data on it.
    VB Code:
    1. this.dataGrid1.DataSource = ds;
    2.  
    3.             if(ds.Tables.Count>0)
    4.                 if(ds.Tables[0].Rows.Count>0)
    5.                     MessageBox.Show("Records found");
    6.                 else
    7.                     MessageBox.Show("No records found");

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Malaysia
    Posts
    345

    Re: Datagrid

    Thanks! It really helped.
    Thanks.

  4. #4
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: Datagrid

    i would make your sub return a boolean. that boolean is determined on whether or not the dataset has data. then, when you call that function, you can display a message box if it returns false.

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: Datagrid

    Code:
    private bool  BindSearchData(string strSearchSql)
    	{
    
              OleDbConnection scnnNW = new OleDbConnection(SQL_CONNECTION_STRING);
    	
    	  OleDbCommand scmd = new OleDbCommand(strSearchSql, scnnNW);
    			
    	  OleDbDataAdapter sda = new OleDbDataAdapter(scmd);
    
    	  DataSet ds = new DataSet();
    
    	  if (sda.Fill(ds) = 0)
    {return false;}
    
    	  dgCompany.DataSource = ds;
    	  dgCompany.DataBind();
    
            }

  6. #6
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: Datagrid

    oops! the code should read like this
    Code:
    private bool  BindSearchData(string strSearchSql)
    	{
    
              OleDbConnection scnnNW = new OleDbConnection(SQL_CONNECTION_STRING);
    	
    	  OleDbCommand scmd = new OleDbCommand(strSearchSql, scnnNW);
    			
    	  OleDbDataAdapter sda = new OleDbDataAdapter(scmd);
    
    	  DataSet ds = new DataSet();
    
    	  if (sda.Fill(ds) == 0)
    {return false;}
    
    	  dgCompany.DataSource = ds;
    	  dgCompany.DataBind();
    
            }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width