Sqlconnection connection = NULL;
Try
{
Try
{
Connection = getconnection (txtconnectionstring. Text );
}
Catch
{
MessageBox. Show ("the connection with the database can be established", "application error", messageboxbuttons. OK, messageboxicon. Error );
Return;
}
// Set up parameters (1 input and 3 Output)
Sqlparameter [] arparms = new sqlparameter [4];
// @ Productid input parameter
// Assign = 1
Arparms [0] = new sqlparameter ("@ productid", sqldbtype. INT );
Arparms [0]. = 1;
// @ Productname output parameter
Arparms [1] = new sqlparameter ("@ productname", sqldbtype. nvarchar, 40 );
Arparms [1]. Direction = parameterdirection. output;
// @ Unitprice output parameter
Arparms [2] = new sqlparameter ("@ unitprice", sqldbtype. Money );
Arparms [2]. Direction = parameterdirection. output;
// @ Qtyperunit output parameter
Arparms [3] = new sqlparameter ("@ qtyperunit", sqldbtype. nvarchar, 20 );
Arparms [3]. Direction = parameterdirection. output;
// Call executenonquery static method of sqlhelper class
// We pass in database connection string, command type, stored procedure name and an array of sqlparameter objects
Sqlhelper. executenonquery (connection, commandtype. storedprocedure, "getproductdetails", arparms );
// Display results in text box using the s of output parameters
Txtresults. Text = arparms [1]. + "," + arparms [2]. + "," + arparms [3].;
}
Catch (exception ex)
{
String errmessage = "";
For (exception tempexception = ex; tempexception! = NULL; tempexception = tempexception. innerexception)
{
Errmessage + = tempexception. Message + environment. newline + environment. newline;
}
MessageBox. Show (string. Format ("there are some problems while trying to use the data access application block, please check the following error messages: {0 }"
+ Environment. newline + "This test requires some modifications to the northwind database. please make sure the database has been initialized using the setupdatabase. BAT database, or from the install Quickstart option on the Start Menu. ", errmessage ),
"Application error", messageboxbuttons. OK, messageboxicon. Error );
}
Finally
{
If (connection! = NULL)
Connection. Dispose ();
}