Thursday, 26 June 2014

Find Device Plugged In Or Not


    Public Shared Function Populate_list() As Integer
        Dim searcher As New ManagementObjectSearcher("SELECT Name FROM  Win32_pnpentity where Name like 'Concel%'")
        Return searcher.Get().Count
    End Function
 



If Store.Populate_list() = 0 Then
            MsgBox("Diacorder Not Connected Properly." & vbCrLf & "Connect It Properly and Then Proceed Further.")
            Return
        End If
        If funcs.CheckIfRunning("Diacorder") = True Then funcs.KillProcess("Diacorder")

Tuesday, 24 June 2014

Crystal Reports In Windows Forms With Parameters

In this example i am explaining how to create Crystal Reports In Winforms Or Windows Forms Application With Parameters from user to filter report using C#.NET and VB.NET 


Crystal Reports In Winforms Or Windows Forms Application With Parameters
For this i have created two tables in database named Employees and Projects and fetching data from both tables

I've grouped results by Department name using group expert in crystal reports and put a dropdown on the form to select project name to display related report.

Employee table schema 

ID    int   
FirstName    varchar(50)
LastName    varchar(50)    
Department    varchar(50)    
ProjectID    numeric(18, 0)   
Expenses    money    


Projects table schema 

ProjectID    numeric(18, 0)    
ProjectName    varchar(50)   






Create a new project in VS and go to solution explorer and add new item > crystal report.

Select Blank report option from the wizard window
reate a new project in VS and go to solution explorer and add new item > crystal report.
Select Blank report option from the wizard window

 
Now click on CrystalReports menu and select DataBase Expert 
Now in next window expand Create new connection section and OLEDB(ADO) and in next window select SQL Native Client
Enter you SQL Server name , username and password , select database name from the dropdown and click on ok
In next window expand to find your tables and add them in right pane
Click OK to finish

Now Right Click on Group Name Fields in Field Explorer and Select Group Expert.
In group expert box select the field on which you want data to be grouped.

 
  
Design your report by dragging the fields in section3 (Details) 
my design look like this  
In the form add a combobox and drag and drop CrystalReport Viewer from toobox. click on smart tag and choose the report we created earlier (CrystalReport1.rpt) 
Form look like this 
When we build and rum this report , it asks for Database login username and password , we need to provide database username and password in code behind.
 we need to write code in code behind to filter report based on user selected value or value provided by user .
VB.NET code behind
//Code to populate dropdown
//Fill dropdown in form_Load event by calling 
//function written below
private void FillDropDown()
{
 SqlConnection con = new SqlConnection
       (ConfigurationManager.AppSettings["myConnection"]);
 SqlCommand cmd = new SqlCommand
("Select distinct ProjectID,ProjectName from Projects", con);
 con.Open();
 DataSet objDs = new DataSet();
 SqlDataAdapter dAdapter = new SqlDataAdapter();
 dAdapter.SelectCommand = cmd;
 dAdapter.Fill(objDs);
 cmbMonth.DataSource = objDs.Tables[0];
 cmbMonth.DisplayMember = "ProjectName";
 cmbMonth.ValueMember = "ProjectID";
 cmbMonth.SelectedIndex = 0;
}
private void cmbMonth_SelectedIndexChanged
              (object sender, EventArgs e)
{
      //Create object of report 
CrystalReport1 objReport = new CrystalReport1();

    //set database login information
objReport.SetDatabaseLogon
    ("amit", "password", @"AVDHESH\SQLEXPRESS", "TestDB");

//write formula to pass parameters to report 
crystalReportViewer1.SelectionFormula 
    ="{Projects.ProjectID} =" +cmbMonth.SelectedIndex;
crystalReportViewer1.ReportSource = objReport;
}
      
  






Copy a table across two database in the same Sql Server.

select into [destination database.dbo.destination table]

from [source database.dbo.source table]
 
Example:
select into Mydatabase2.dbo.employee_backup
from mydatabase1.dbo.employee

Sql Server: How to copy a table?

This is the simplest way to copy a table into another (new) table in the same SQL Server database. This way of copying does not copy constraints and indexes.




select * into [destination table] from [source table]
Example:
Select * into employee_backup from employee

We can also select only a few columns into the destination table like below

select col1, col2, col3 into [destination table]from [source table]
Example:
Select empId, empFirstName, empLastName, emgAge into employee_backup fromemployee

Use this to copy only the structure of the source table.

select * into [destination table] from [source table] where 1 = 2
Example:
select * into employee_backup from employee where 1=2

Use this to copy a table across two database in the same Sql Server.





select * into [destination database.dbo.destination table] from [source database.dbo.source table]
Example:
select * into Mydatabase2.dbo.employee_backup
from mydatabase1.dbo.employee