Gridview: Perform CRUD operations in Gridview control in ASP.NET(C# & Vb.NET),

In this article I will explain how to perform all crud functions using Gridview data Control i.e. insert, update, delete and select operations. In this example I will perform all CRUD operations in gridview control using Row_Command event. 

I have used Template fields to show data and update data in Gridview . I have added textbox controls in footer Template of Gridview to enter data.

Demo:
Click on Image to Enlarge it
FTP: How to download files from FTP server in asp.net using C#?

FTP: How to download files from FTP server in asp.net using C#?

Introduction:
In this article I will explain how to download files from FTP server in asp.net using c#. We can download any type of file from server using this code. In last article I have explained how to download CSV files from FTP server.


Code to Download .CSV or .XLSX Files from FTP Server:

protected void Page_Load(object sender, EventArgs e)
    {
        string FtpServer="ftp://--.--.--.- /";
        string username="username";
        string password="password";
        string localpath=@"D:\\FolderName\";
        DownloadFile(FtpServer,username,password,localpath);               
           
    }

 public void DownloadFile(string FtpServer,string username,string password,string localpath)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FtpServer);
        request.Credentials = new NetworkCredential(username,password );
        request.Method = WebRequestMethods.Ftp.ListDirectory;
        StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
        string fileName3 = streamReader.ReadLine();
        List<string> directories = new List<string>();

        while (fileName3 != null && fileName3 != "testing12")
        {            
            directories.Add(fileName3);
            fileName3 = streamReader.ReadLine();

        }

        streamReader.Close();


        using (WebClient ftpClient = new WebClient())
        {
            ftpClient.Credentials = newSystem.Net.NetworkCredential(username , password );

            for (int i = 0; i <= directories.Count - 1; i++)
            {
                if (directories[i].Contains("."))
                {

                    string path = FtpServer + directories[i].ToString();
                    string trnsfrpth = localpath + directories[i].ToString();
                    ftpClient.DownloadFile(path, trnsfrpth);
                }
            }
        } 
       
    }


How to download all CSV or XLSX  files from FTP server in ASP.NET using C#?

How to download all CSV or XLSX files from FTP server in ASP.NET using C#?

Introduction:

Recently I have gone through the requirement of downloading all CSV or XLSX on FTP server in asp.net using c#. I have share the code how I have solve this issue. We can download any type of file from server using this code. Just change the Extension of the file or mention multiple files extension.

Code to Download .CSV or .XLSX Files from FTP Server:

protected void Page_Load(object sender, EventArgs e)
    {
        string FtpServer="ftp://--.--.--.- /";
        string username="username";
        string password="password";
        string localpath=@"D:\\FolderName\";
        DownloadFile(FtpServer,username,password,localpath);               
           
    }

 public void DownloadFile(string FtpServer,string username,string password,string localpath)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FtpServer);
        request.Credentials = new NetworkCredential(username,password );
        request.Method = WebRequestMethods.Ftp.ListDirectory;
        StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
        string fileName3 = streamReader.ReadLine();
        List<string> directories = new List<string>();

        while (fileName3 != null && fileName3 != "testing12")
        {
            if (Path.GetExtension(fileName3) == ".csv"//or .xlsx
            {
                directories.Add(fileName3);
            }

            fileName3 = streamReader.ReadLine();

        }

        streamReader.Close();


        using (WebClient ftpClient = new WebClient())
        {
            ftpClient.Credentials = newSystem.Net.NetworkCredential(username , password );

            for (int i = 0; i <= directories.Count - 1; i++)
            {
                if (directories[i].Contains("."))
                {

                    string path = FtpServer + directories[i].ToString();
                    string trnsfrpth = localpath + directories[i].ToString();
                    ftpClient.DownloadFile(path, trnsfrpth);
                }
            }
        } 
       
    }

ASP.NET: Merge two data-tables into one in asp.net using c# & vb.net.

Some time while developing application in asp.net , it is required to merge two datatables. It can be achieved by using Merge function of Datatable in asp.net.

In this article I will explain how to merge two data tables in to one.

Public DataTable merge_table()
{
Var tbl = GetTable1();
tbl.Merge(GetTable2());
return tbl;
}


Demo:
Click on image to Enlarge it

FormView: Select, Insert, update and delete data in Form View using SqlDataSource.

Introduction: Form View data control can be used to Display, Insert, Delete and update data. We can insert data to database using FormView and also update or delete data. There is no need to write code to do this. We can achieve this by using SQLDataSource control. We write Insert, Delete, update command in SQLDataSource and pass parameter.


Demo:

ASP.NET: Configure or Querying SqlDataSource to database for CRUD with DataControls.

In this article I will explain how to use SqlDataSource data control as a datasource to different data controls: Gridview, form view, details View etc.

I will explain how to Configure SqlDataSource i.e. connect to database and Create different Commands such as Select Command, insert Command, update and Delete Command.

With  SqlDataSource we can select, insert delete and update data using different data Controls.
Description:


Configure SqlDataSource:

1.      Add SQLDataSource on the webform after data control (gridview, formview or details view) by drag and drop.

<asp:SqlDataSource ID="SqlDataSource2" runat="server"></asp:SqlDataSource>

 In Design section:



2.      Click Configure Data Source: following page will be displayed


3.      Click New Connection button and Create connection with Database:


4.      Now Press Next  button after creating connection


5.      Save Connection string Name to Configuration File and Press ok:


6.      Configure the Select Statement. There will be two options, select first option if you want to create custom SQL Statement for Select, insert, Update and delete. Then Press Next:


7.      In Next Page Define custom statements for different tasks. We can create query manually or using Query builder. 
Query For Update:


8.      Query for Insert:

9.      Query For Delete:

10.  Query for Select:

11.   After creating all queries press Next Button. Next button will be enabled only after creating all queries.
12.  Test query. This will be the last step. If you want to test query then you can test here. Then press Finish to complete the configuration.




After configuration step, SqlDataSource Control in source section will be look like:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:BlogConnectionString3 %>" DeleteCommand="DELETE FROM Student_info WHERE (Student_Id = @Studentid)" InsertCommand="INSERT INTO Student_info(Student_name, Age, Class) VALUES (@name, @age, @class)" SelectCommand="SELECT Student_name, Age, Class FROM Student_info" UpdateCommand="UPDATE Student_info SET Student_name = @name, Age = @age, Class = @class WHERE (Student_Id = @studentid)">
    <DeleteParameters>
        <asp:Parameter Name="Studentid" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="name" />
        <asp:Parameter Name="age" />
        <asp:Parameter Name="class" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="name" />
        <asp:Parameter Name="age" />
        <asp:Parameter Name="class" />
        <asp:Parameter Name="studentid" />
    </UpdateParameters>
                </asp:SqlDataSource>

In this article, I have learned how to Configure SqlDataSource control for select, insert, delete and update through DataControls using SqlDataSource.  If you have any doubt you can ask by leaving your query in comment section.

Kategori

Kategori