Question: ```
  if (rbMale.Checked == false && rbFemale.Checked == false)
        {
            flag = true;
            e.Cancel = true;
            groupBox1.Focus();
            errorProvider1.SetError(groupBox1, "Please Select the gender");
        }
        else
        {
            e.Cancel = false;
            errorProvider1.SetError(groupBox1,"");
        }  

```

When I check one of the radio buttons. It still shows that red icon Why is it showing that error icon. Please check the code and help me out. Any help would be great. Thanks

Answer: 
Here is another option that doesn't use any loops or cursors. I am using sys.database to generate a single select statement. Unless you have a LOT of databases this isn't going to be much, if any, performance boost over a loop based solution. But I really dislike cursors and loops and this is a fairly simple way to build dynamic sql to avoid using a loop.

```
DECLARE @SQL NVARCHAR(MAX) = '' --need to initialize to empty string
    , @TableToFind sysname = 'YourTableName'

SELECT @SQL = @SQL + 'select TABLE_CATALOG, TABLE_NAME, COLUMN_NAME, DATA_TYPE from [' + db.name + '].INFORMATION_SCHEMA.COLUMNS  where TABLE_NAME = @TableToFind UNION ALL '
FROM sys.databases db
WHERE [state] = 0 --only gets online multi user databases
    AND name LIKE 'myDBname_%'

IF LEN(@SQL) > 0
BEGIN
    SELECT @SQL = LEFT(@SQL, LEN(@SQL) - 10)

    SELECT @SQL
    --uncomment the line below when you are comfortable the dynamic sql generated will function.
    --EXEC sp_executesql @SQL, N'@TableToFind sysname', @TableToFind = @TableToFind
END

```
Use a dynamic sql in a loop to fetch DB Names or use dynamic sql with cursor

Method 1: Using Loop

```
Declare @ssql nvarchar(max),@DBName varchar(100),@rowcount int,@datatype varchar(10)

Create table #temp(id int identity(1,1) ,DBName varchar(100))

set @rowcount=0
set @datatype='int'

Insert into #temp(DBName)
SELECT [name]  FROM sys.[sysdatabases] WHERE [name] like 'MyDBName%'

while @rowcount<=1
begin

select @DBName=DBName from #temp where id=@rowcount

set @ssql=N'select TABLE_CATALOG,TABLE_NAME, COLUMN_NAME, DATA_TYPE from '+@DBName+'.INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME in (select TABLE_NAME from '+@DBName+'.INFORMATION_SCHEMA.TABLES where TABLE_TYPE=''BASE TABLE'')
and DATA_TYPE='''+@datatype+''''

--select @ssql
exec sp_executesql @ssql

set @rowcount=@rowcount+1
end

```

Method 2: Cursor

```
    Declare @ssql nvarchar(max),@DBName varchar(100),@rowcount int,@datatype varchar(10)
set @rowcount=0
set @datatype='int'
DECLARE cursorDBName CURSOR -- Declare cursor
FOR

SELECT [name]  FROM sys.[sysdatabases] WHERE [name] like 'MyDBName%'
OPEN cursorDBName -- open the cursor

FETCH NEXT FROM cursorDBName

 INTO @DBName

WHILE @@FETCH_STATUS = 0

BEGIN

 set @ssql=N'select TABLE_CATALOG,TABLE_NAME, COLUMN_NAME, DATA_TYPE from '+@DBName+'.INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME in (select TABLE_NAME from '+@DBName+'.INFORMATION_SCHEMA.TABLES where TABLE_TYPE=''BASE TABLE'')
and DATA_TYPE='''+@datatype+''''

--select @ssql
exec sp_executesql @ssql

 FETCH NEXT FROM cursorDBName

   INTO @DBName
END

CLOSE cursorDBName -- close the cursor

DEALLOCATE cursorDBName 

```