Sample ASP.NET Code with C#

 

Product:

HiT OLEDB Server/400, HiT OLEDB/400

Version:

All versions

Category:

Tools and Applications

Last Updated:

Topic ID:

03/26/2003

KBFAQ 1372

Summary:

Creating an ASP.NET connection with C#

 

Q.

How do I call a stored procedure on DB2 from ASP.NET using the HiT OLEDB driver?

 

A.

Here is a sample ASP.NET C# code with an AS/400 stored procedure call:

 

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.OleDb" %>

<script language="c#" runat="server">

  void Page_Load(object sender, EventArgs e)

  {

    OleDbConnection objConnection = null;

    OleDbCommand objCommand = null;

    OleDbParameter parm;

    string strConnect;

 

            //Create and Open a connection

    strConnect = @"Provider=HiTOLEDB400.1;Password=MyPassword;";

    strConnect +=@"Persist Security Info=True;";

strConnect +=@"User ID=MyUser;Data Source=100.100.100.100";

 

    objConnection = new OleDbConnection(strConnect);

    objConnection.Open();

   

    //Create the command object for the stored procedure

   

    objCommand = new OleDbCommand();

    objCommand.Connection = objConnection;

    objCommand.CommandText = "CALL MyLib.MyStoredProcedure(?)";

    objCommand.CommandType = System.Data.CommandType.Text;

           

    parm = objCommand.Parameters.Add("uno", OleDbType.Integer);

    parm.Direction = ParameterDirection.Input;

    parm.Value = 1;

           

    MyDataGrid.DataSource = objCommand.ExecuteReader(CommandBehavior.CloseConnection);

    MyDataGrid.DataBind();   

 

   }

</script>

<html>

<head>

  <title>Calling Stored Procedure</title>

</head>

<body>

<h2>Calling Stored Procedure</h2>

  <asp:DataGrid id="MyDataGrid" runat="server"  />

</body>

</html>

 

If your Internet Information Server is on C drive, save the code above into a file under "C:\Inetpub\wwwroot". Make sure to give it an aspx extension. For example, "MyASPNET.aspx".

From the Internet Information Server, locate the file, right-click and select Browse from the contextual menu. Please refer to the screenshot below: