Tuesday, December 2, 2008

Using Crystal Reports in ASP .NET

Imports CrystalDecisions.CrystalReports.Engine

Dim ds as new Dataset
'Fill the dataset using Data adapter

Dim rep As New ReportDocument
rep.Load(Server.MapPath("CrystalReport1.rpt"))

rep.setDatasource(ds)

CrystalReportViewer1.ReportSource = rep

Thursday, November 20, 2008

MS SQL Restore and Backup code using .NET

First add a reference of Microsoft SQLDMO Object Library from COM tab .Then Write the below code
//Function for Restore

private void Restore()
{
try
{
this.Cursor = Cursors.WaitCursor;
//create an instance of a server class
SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass();
//connect to the server
srv.Connect("(local)","sa",""); //srv.Connect(servername,userid,password)
//create a restore class instance

SQLDMO.Restore res = new SQLDMO.RestoreClass();
//set the backup device = files property ( easy way )
res.Devices = res.Files;
//set the files property to the File Name text box
res.Files =textBox1.Text;//give full path with restore filename
//set the database to the chosen database
res.Database = "databaseName";
// Restore the database

res.ReplaceDatabase = true;
res.SQLRestore(srv);

MessageBox.Show("Database restored successfully.", "Restore Successfull");
this.Cursor = Cursors.Default;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"Error");
}
}


//Function for Backup

private void BackUp()
{
try
{
Class1.con.Close();
this.Cursor = Cursors.WaitCursor;
//create an instance of a server class
SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass();
//connect to the server

srv.Connect("(local)","sa",""); //srv.Connect(servername,userid,password)
//create a backup class instance
SQLDMO.Backup bak = new SQLDMO.BackupClass();
//set the backup device = files property ( easy way )
bak.Devices = bak.Files;
//set the files property to the File Name text box
//
bak.Files = this.textBox1.Text;//give full path with bakup filename

//set the database to the chosen database
bak.Database = "databasename";
//perform the backup
bak.SQLBackup(srv);
MessageBox.Show("Database successfully backed up.", "Backup Successfull");
this.Cursor = Cursors.Default;
}
catch(Exception err)
{
this.Cursor = Cursors.Default;
MessageBox.Show(err.Message,"Error");
}
}

Saturday, November 15, 2008

Using Date Field in .NET when using with MS Access


Use #"+dateTimePicker1.Values+"# instead of
'"+dateTimePicker1.Values+"'

This type is only used when retriving values using date or updating using date or deleting using date

NOTE: This type is not used while inserting datas to table

Inserting single quotes to a string in sql query

Just Add one more single quotes to that string by the user

eg: insert into table name values('D''zousa')

or

The programmer has to maintain that one using coding
Means we have to check the string if a single quotes exists or not.
If exists we have to add one more single quotes to that string.s

GETTING VALUES FROM DATASET

GETTING VALUES FROM DATASET

txtName.text = ds.Tables(0).Rows(row).Item(1)

Making Changes to Dataset Manually

ds.Tables(0).Rows(row).Item(1) = dt1.Value.ToShortTimeString()
ds.Tables(0).Rows(row).Item(2) = dt2.Value.ToShortTimeString()
ds.Tables(0).Rows(row).Item(3) = dt3.Text
ds.Tables(0).Rows(row).Item(4) = dt4.Text
ds.Tables(0).Rows(row).Item(5) = fin.Hours
ds.Tables(0).Rows(row).Item(6) = fin.Minutes


This is one method.There are so many other methods also; by using DataRow and DataTable etc..

CODE FOR POPULATING COMBOBOX USING DATASET

Dim ds as new Dataset
Dim sda as new OleDBDataAdapter("write query here",connection)

sda.Fill(ds,"tablename")

With cmbEOS
.DataSource = ds.Tables("Tablename")
.ValueMember = "fieldname
End With

DATAGRIDVIEW MOVEMENT THROUGH COLUMN WHEN PRESSING ENTER KEY



USING VB .NET

'Declare row,col as public

Dim row, col As Integer


'Write the below code in CellEndEdit Event

Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

row = e.RowIndex
col = e.ColumnIndex

End Sub


'Write the below code in DataGridView SelectionChanged Event [ SAY DATAGRIDVIEW HAS 4 COLUMNS ]

Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged

Try
Dim sl As Integer
sl = row

If DataGridView1.Rows.Count > 1 Then

Select Case col

Case 0, 1, 2

DataGridView1.CurrentCell = Me.DataGridView1(col + 1, row)

Case 3

DataGridView1.CurrentCell = Me.DataGridView1(0, row + 1)

End Select

End If


Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub


Click here for coverting VB .NET to C#.NET and viseversa