C#

How to force download a file using ASP.NET

There are many cases where we need to let file downloads being processed by an aspx file. Here in this code this code will force a file to be downloaded through an aspx file. let assume your file path  is in “string path” variable. Add this code in your aspx file cs file.

 


string path = "the path to your file";
string fileName = "Add your file name";
string contentType= "Add your file content type. ex: for text file it is 'text/plain'";
string contentLength = "add string presentation of your file size in bytes";

response.Clear();
response.ClearHeaders();
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
response.AddHeader("Content-Length", contentLength);
response.ContentType = contentType;
FileStream f = new FileStream(path, FileMode.Open);
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = f.Read(buffer, 0, buffer.Length)) > 0)
{
    response.OutputStream.Write(buffer, 0, len);
}
response.OutputStream.Flush();
response.OutputStream.Close();
response.Flush();
response.Close();
response.End();
No Comments

Showing database result in dataGridView in C#

First use this following link to know how to connect with oracle database using C#.

Now create a user form or control from the solution explorer. From the toolbox drag and drop a datagridview in your form. Right click the datagrid view in and go to properties. Change its name property from datagridview1 to table. Remember this is just the name of the variable for datagridview. So you can name it just as we wish. But here I am using “table”

Now use the following code to fill the datagridview with sql query.

1 Comment

Read more

Calling oracle procedures using C#

I have this procedure stored in oracle database.

-- procedure to to calculate student cgpa/grade letter of whole semester
create or replace procedure calc_sem_cgpa(regIn in number, semIn varchar2)
as
    cursor result is
    select cgpa.gpa, syllabus.credit
    from cgpa,syllabus,student
    where
    cgpa.reg = regIn
    and cgpa.semester = semIn
    and cgpa.course = syllabus.course
    and cgpa.semester = syllabus.semester
    and student.reg = cgpa.reg
    and student.d ept = syllabus.dept
    and student.ses = syllabus.ses;

    eachResult result%rowtype;

    sum_cgpa   number(5,-2)  := 0;
    sum_credit  number(5,2) := 0;
    final_cgpa number(4,2)  := 0;
    countRow number(2)      := 0;

begin
    open result;
    loop
        fetch result into eachResult;
        exit when result%notfound;

        if eachResult.gpa > 0.0 then
            sum_cgpa  := sum_cgpa + eachResult.gpa * eachResult.credit;
            sum_credit := sum_credit + eachResult.credit;
         end if;

    end loop;

    if sum_credit > 0 then
        final_cgpa := round(sum_cgpa/sum_credit, 2);
    else
        final_cgpa := 0;
    end if;

    select count(*) into countRow from cgpa_sem
    where cgpa_sem.reg = regIn
    and cgpa_sem.semester = semIn;

    if countRow > 0 then
        update cgpa_sem set cgpa = final_cgpa, credits_comp = sum_credit
        where reg = regIn and semester = semIn;
    else
        insert into cgpa_sem(reg, semester, cgpa, credits_comp)
        values(regIn, semIn, final_cgpa, sum_credit);
    end if;
end;
/
No Comments

Read more

How to connect C# with oracle

This time I am gonna work with my database project. This course was on oracle database. So now I must connect my software interface with oracle. So first try’t with C#. This is how I did it

To connect to oracle database using C# first you what you must do is to install Oracle Database and Microsoft Visual Studio in your computer.
You also should setup oracle cilent in order to have them work with each other.

No Comments

Read more

Connect to Microsoft Access .mdb database using C#

Here is a simple example that will tell you how to connect to microsoft access (mdb) files in C# .NET. To do this you have to make use OLEDB connector

The first you need is to use OLEDB connector.

First we need to use oledb connector namespace.

using System.Data.OleDb;

Now we need to create an instance of OleDbConnection object. The following will be the connection string required to use as parameter of OleDbConnection instance object

2 Comments

Read more

Connect to mysql database using C#

I searched Internet a lot before I could figure out how to connect to mysql database server using C#. This is the one that worked in my favour.

To connect to mysql database using C# first you should have to download a small program called mysql connector .net from MySQL’s official site. Click here to visit this link to download mysql connector .net

4 Comments

Read more