Tag Archives: programing

How to use common folder paths in C#


Reading files from application path may be one of key requirement of any application. Actually we can place many place you love to have, on a installation machine there can be restriction as well.

Like wise in a Windows system, installer may want to put apps and other files to Programfiles. This folder intended to work with executable,configuration files etc. You can’t programmatically create new folder or files in Programfiles folder, which may require some admin rights. So where we kept those documents and other data ?

It is wise to store such files on Application data folder or even on your Document folder. For reports and other stuffs Document folder is enough.

Access Special Folders

We can use the Enironment.SpecialFolder to access the App data and Documents and also can fetch path to those folders using GetFolderPath method of Environment class.

datapath = Environment.GetFolderPath(Environment.SpecialFolder
.ApplicationData);
reportPath = Environment.GetFolderPath(Environment.
SpecialFolder.MyDocuments);

the first line will get the location of C:\Users\manoj\AppData\Roaming and the second will show the Document folder path .

Access application path

How to access application/executable path in C#. This can be done using Application.ExecutablePath and the GetFolderPath method

 Path.GetDirectoryName(Application.ExecutablePath);

How to delete rows using simple sql statement in C#


As we have seen how to delete rows in a dataset  in C#.Net, there are simple ways to do the same using SQL command object. All we need is an sql statement and an Object to do the job.

con.open();Common.cmd = new SqlCommand("delete from purchasepart where billno=" + b1, Common.con);

Common.cmd.ExecuteNonQuery();con.close();

cmd object configure the delete statement and call the ExecuteNonQuery() , and will delete those rows you are specified. Keep in mind that the connection should kept open while the ExecuteNonQuery(); method invoked.

Inserting Data rows with Parameters and Stored procedure in C# and MS SQL Server


In C# you can do your SQL things in many ways, one of the safest and fastest method in execute SQL commands from SQL server itself. Suppose you want to new row to Product table,

  1. First you need to setup SqlDataAdapter
  2. Create Parameter Objects
  3. Create SQL Procedure with Parameters
Lets do one by one

SqlDataAdapter

con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings
["MACCon"].ConnectionString;
 con.Open();
 ProductTableAdapter = new SqlDataAdapter("select * from productMaster", con);
We are now accessed the Product table with SqlDataAdapter and ready to create Parameter sets.

Setting up Parameters with values

ProductTableAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
 ProductTableAdapter.SelectCommand.CommandText = "CRUDS_PRODUCTMASTER";
 ProductTableAdapter.SelectCommand.Parameters.Add(new SqlParameter("@Item", cmb_item.Text.ToString().ToUpper()));
 ProductTableAdapter.SelectCommand.Parameters.Add(new SqlParameter("@mfr", cmb_mfr.Text.ToString().ToUpper())); Common.ProductTableAdapter.SelectCommand.ExecuteScalar();
with the SqlParameter  we can pass values to the SQL Procedure that we are going to create in next step. The commandType of stored procedure indicate the it is StoredProcedure. Now all we need is a Procedure.

Design your  SQL Stored Procedure

Open you SQL Server Explorer or SQL Server Itself and expand Procedure node and drop the following line of code.
ALTER PROCEDURE dbo.CRUDS_PRODUCTMASTER
 (
 @item nchar(10),
 @mfr nchar(10),
 )
 AS
 Begin
 insert into productmaster  (item,mfr) values(@item,@mfr,);
 End
The parameters in C# and SQL should match otherwise it will cause errors.

VB6: List all printers using code in Windows


Listing all printers with name, Port in VB6 is simple as listing default printer port using Kernel32 library . Today we going show you how to do this.

As usual we start with function declaration, then define a new function which add all information into a list box. Here we goes

Kernel32 GetProfileString method

Declare Function GetProfileString Lib “kernel32.dll” Alias “GetProfileStringA” (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Add printer information into a list box

Public Sub GetPrinterList(lstPrinter As ListBox)Dim PrintData As PrinterDim defprinterpos%

For Each PrintData In Printers

‘ Add printer name and port to list

lstPrinter.AddItem PrintData.DeviceName & ” at: ” & PrintData.Port

‘ Check for default printer

If PrintData.DeviceName = Printer.DeviceName Then defprinterpos = lstPrinter.NewIndex

Next

lstPrinter.ListIndex = defprinterpos%

End Sub

This sub procedure will take list box as argument, and add printer to this list box.

Call the sub

The final line of code just invoke the procedure and see what we have.

Happy coding

Add new fields to Access table using VB6


Let me show how to add a new field into an existing access database using Visual Basic 6.0.Using the tableDef and Filed object you can create new fields. Firstly, you need to create Database and Recordset object, mke sure the DAO access object library. Continue reading