I have a listview which is adding some products in the database. If i insert one product, it works perfectly. The problem with this code is the cmd2.Parameters.Clear(); If i insert more than one product in the form listview, it only sends the last one to the database. How can i do to insert all products, because it seems that if i don't use parameters.clear, it's not working at all. There is no error thrown, it just does not insert products.
This is the code:
private void InsertOrder_Click(object sender, EventArgs e)
{
//---------------Inserare client--------------------
SqlCommand cmd = new SqlCommand("InsertClients", con);
cmd.CommandType = CommandType.StoredProcedure;
if (TextBoxClientNou.Enabled)
{
cmd.Parameters.AddWithValue("#NumeClient", TextBoxClientNou.Text);
}
else
{
cmd.Parameters.AddWithValue("#NumeClient", ClientExistent.Text);
}
var IDClientParameter = cmd.Parameters.Add("#IDClient", SqlDbType.Int);
IDClientParameter.Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
var IDClient = (int)IDClientParameter.Value;
con.Close();
//--------------------Inserare Produse------------------
SqlCommand cmd2 = new SqlCommand("InsertProducts", con);
cmd2.CommandType = CommandType.StoredProcedure;
foreach (ListViewItem item in ListaProduse.Items)
{
cmd2.Parameters.Clear(); //Problem here
cmd2.Parameters.AddWithValue("#Denumire", item.Text);
cmd2.Parameters.AddWithValue("#Cantitate", item.SubItems[1].Text);
cmd2.Parameters.AddWithValue("#Dimensiuni", item.SubItems[1].Text);
cmd2.Parameters.AddWithValue("#Comentarii", item.SubItems[1].Text);
}
var IDProductParameter = cmd2.Parameters.Add("#IDProdus", SqlDbType.Int);
IDProductParameter.Direction = ParameterDirection.Output;
con.Open();
cmd2.ExecuteNonQuery();
var IDProdus = (int)IDProductParameter.Value;
con.Close();
//--------------------Inserare comanda-------------------
SqlCommand cmd3 = new SqlCommand("InsertOrders", con);
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.AddWithValue("#IDClient", IDClient);
cmd3.Parameters.AddWithValue("#IDProdus", IDProdus);
cmd3.Parameters.AddWithValue("#DataInceput", dateTimePicker1.Text);
cmd3.Parameters.AddWithValue("#DataSfarsit", dateTimePicker2.Text);
cmd3.Parameters.AddWithValue("#Facturata", factstatus);
cmd3.Parameters.AddWithValue("#Livrata", livstatus);
con.Open();
cmd3.ExecuteNonQuery();
con.Close();
you have to execute the query inside your loop, cause you're adding parameters in the loop (if two ListViewItem so 8 parameters), that's why you're getting this error.
foreach (ListViewItem item in ListaProduse.Items)
{
SqlCommand cmd2 = new SqlCommand("InsertProducts", con);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("#DenProd", item.Text);
cmd2.Parameters.AddWithValue("#ProdQuant", item.SubItems[1].Text);
cmd2.Parameters.AddWithValue("#ProdSize", item.SubItems[1].Text);
cmd2.Parameters.AddWithValue("#ProdComm", item.SubItems[1].Text);
var IDProductParameter = cmd2.Parameters.Add("#ProdID", SqlDbType.Int);
IDProductParameter.Direction = ParameterDirection.Output;
con.Open();
cmd2.ExecuteNonQuery();
var IDProdus = (int)IDProductParameter.Value;
con.Close();
}
You are adding the parameters without clearing the originals. You can either use Parameters.Clear() or you can change the Value of existing parameters. You then need to execute the command within the loop.
Note that you must use using blocks to dispose the connection. And if you do so, the connection will be closed automatically
Specify the parameter types and lengths explicitly
var listIds = new List<int>();
using (var con = new SqlConnection(YourConnString))
using (var cmd2 = new SqlCommand("InsertProducts", con) { CommandType = CommandType.StoredProcedure })
using (var cmd3 = new SqlCommand("InsertOrders", con) { CommandType = CommandType.StoredProcedure })
{
cmd2.Parameters.Add("#DenProd", SqlDbType.NVarChar, 50);
cmd2.Parameters.Add("#ProdQuant", SqlDbType.NVarChar, 50);
cmd2.Parameters.Add("#ProdSize", SqlDbType.NVarChar, 50);
cmd2.Parameters.Add("#ProdComm", SqlDbType.NVarChar, 50);
var IDProductParameter = cmd2.Parameters.Add("#ProdID", SqlDbType.Int);
IDProductParameter.Direction = ParameterDirection.Output;
cmd3.Parameters.Add("#IDClient", SqlDbType.Int).Value = IDClient;
cmd3.Parameters.Add("#IDProdus", SqlDbType.Int);
cmd3.Parameters.Add("#DataInceput", SqlDbType.DateTime).Value = dateTimePicker1.Text;
cmd3.Parameters.Add("#DataSfarsit", SqlDbType.DateTime).Value = dateTimePicker2.Text;
cmd3.Parameters.Add("#Facturata", SqlDbType.NVarChar, 50).Value = factstatus;
cmd3.Parameters.Add("#Livrata", SqlDbType.NVarChar, 50).Value = livstatus;
con.Open();
foreach (ListViewItem item in ListaProduse.Items)
{
cmd2.Parameters["#DenProd"].Value = item.Text;
cmd2.Parameters["#ProdQuant"].Value = item.SubItems[1].Text;
cmd2.Parameters["#ProdSize"].Value = item.SubItems[1].Text;
cmd2.Parameters["#ProdComm"].Value = item.SubItems[1].Text;
cmd2.ExecuteNonQuery();
var IDProdus = (int)IDProductParameter.Value; // do something with this value
cmd3.Parameters.AddWithValue("#IDProdus", IDProdus);
cmd3.ExecuteNonQuery();
}
}
I must say, ideally you should use either a Table Valued parameter or SqlBulkCopy to do this in bulk, it will not be performant for large numbers of rows.
I created a Class name EmployeeDAta write this code here and i want to Insert Radiobutton value in SQL Database
public static void AddEmployee(Employee employee)
{
string connString = ConfigurationManager.ConnectionStrings["Employee"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
using (conn)
{
SqlCommand cmd = new SqlCommand("ADDEMPLOYEE", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#Name", SqlDbType.NVarChar, 50).Value = employee.Name;
cmd.Parameters.Add("#FName", SqlDbType.NVarChar, 50).Value = employee.Fname;
cmd.Parameters.Add("#Address", SqlDbType.NVarChar, 50).Value = employee.Address;
cmd.Parameters.Add("#Email", SqlDbType.NVarChar, 50).Value = employee.Email;
cmd.Parameters.Add("#Mobile", SqlDbType.NVarChar, 50).Value = employee.Mobile;
cmd.Parameters.Add("#Pincode", SqlDbType.NVarChar, 50).Value = employee.Pincode;
cmd.Parameters.AddWithValue("#VB", SqlDbType.Bit).Value = employee.VB;
cmd.Parameters.AddWithValue("#ASP", SqlDbType.Bit).Value = employee.ASP;
cmd.Parameters.AddWithValue("#Gender", SqlDbType.Int).Value = employee.Gender;
conn.Open();
cmd.ExecuteNonQuery();
}
}
add a parameter to your stored procedure call:
cmd.Parameters.AddWithValue("#ValueOfRadioButtonr", SqlDbType.Int).Value = MyRadioButton.Value;
In the stored procedure on the database, make sure you handle the extra parameter.
Trying to insert a collection into a table in SQL Server 2014. Here's my code it executes without error but when I check my table - nothing is added. My collection object orders isn't empty either I can see it has 3 members when debugging it.
IEnumerable<CompleteOrderDetails> orders;
JoinDetails(doc, ns, xmlFragment1, out orders);
string connectionstring = null;
SqlConnection conn;
connectionstring = "Data Source = DANNY; Initial Catalog = Alliance; Integrated Security = SSPI";
using (conn = new SqlConnection(connectionstring))
{
string customerInsert = "INSERT INTO AmazonCustomer (AddressLine1, AddressLine2, AddressLine3, City, StateOrRegion, AmazonOrderId, PostalCode, Title, ItemPrice, ShippingPrice, Quantity) " +
"VALUES (#AddressLine1, #AddressLine2, #AddressLine3, #City, #StateOrRegion, #AmazonOrderId, #PostalCode, #Title, #ItemPrice, #ShippingPrice, #Quantity)";
using (SqlCommand query = new SqlCommand(customerInsert))
{
query.Connection = conn;
foreach (var order in orders)
{
query.Parameters.Add("#AmazonOrderId", SqlDbType.NVarChar, 150).Value = order.AmazonOrderId;
query.Parameters.Add("#Name", SqlDbType.NVarChar, 150).Value = order.Name;
query.Parameters.Add("#AddressLine1", SqlDbType.NVarChar, 150).Value = order.AddressLine1;
query.Parameters.Add("#AddressLine2", SqlDbType.NVarChar, 150).Value = order.AddressLine2;
query.Parameters.Add("#AddressLine3", SqlDbType.NVarChar, 150).Value = order.AddressLine3;
query.Parameters.Add("#City", SqlDbType.NVarChar, 150).Value = order.City;
query.Parameters.Add("#StateOrRegion", SqlDbType.NVarChar, 150).Value = order.StateOrRegion;
query.Parameters.Add("#PostalCode", SqlDbType.NVarChar, 150).Value = order.PostalCode;
query.Parameters.Add("#Title", SqlDbType.NVarChar, 150).Value = order.Title;
query.Parameters.Add("#ItemPrice", SqlDbType.NVarChar, 150).Value = order.ItemPrice;
query.Parameters.Add("#ShippingPrice", SqlDbType.NVarChar, 150).Value = order.ShippingPrice;
query.Parameters.Add("#Quantity", SqlDbType.NVarChar, 150).Value = order.Quantity;
}
conn.Open();
conn.Close();
}
}
Code Edits from Comments
private static void ExecuteSqlTransaction(IEnumerable<CompleteOrderDetails> orders)
{
string connectionstring = null;
SqlConnection conn;
SqlTransaction transaction;
connectionstring = "Data Source = DANNY; Initial Catalog = Alliance; Integrated Security = SSPI";
using (conn = new SqlConnection(connectionstring))
{
conn.Open();
transaction = conn.BeginTransaction("Transaction");
string customerInsert =
"INSERT INTO AmazonCustomer (Name, AddressLine1, AddressLine2, AddressLine3, City, StateOrRegion, AmazonOrderId, PostalCode, Title, ItemPrice, ShippingPrice, Quantity) VALUES (#Name, #AddressLine1, #AddressLine2, #AddressLine3, #City, #StateOrRegion, #AmazonOrderId, #PostalCode, #Title, #ItemPrice, #ShippingPrice, #Quantity)";
using (SqlCommand query = new SqlCommand(customerInsert))
{
query.Connection = conn;
query.Transaction = transaction;
query.Parameters.Add("#AmazonOrderId", SqlDbType.NVarChar, 150);
query.Parameters.Add("#Name", SqlDbType.NVarChar, 150);
query.Parameters.Add("#AddressLine1", SqlDbType.NVarChar, 150);
query.Parameters.Add("#AddressLine2", SqlDbType.NVarChar, 150);
query.Parameters.Add("#AddressLine3", SqlDbType.NVarChar, 150);
query.Parameters.Add("#City", SqlDbType.NVarChar, 150);
query.Parameters.Add("#StateOrRegion", SqlDbType.NVarChar, 150);
query.Parameters.Add("#PostalCode", SqlDbType.NVarChar, 150);
query.Parameters.Add("#Title", SqlDbType.NVarChar, 150);
query.Parameters.Add("#ItemPrice", SqlDbType.NVarChar, 150);
query.Parameters.Add("#ShippingPrice", SqlDbType.NVarChar, 150);
query.Parameters.Add("#Quantity", SqlDbType.NVarChar, 150);
try
{
foreach (var order in orders)
{
query.Parameters["#AmazonOrderId"].Value = order.AmazonOrderId ?? Convert.DBNull;
query.Parameters["#Name"].Value = order.Name ?? Convert.DBNull;
query.Parameters["#AddressLine1"].Value = order.AddressLine1 ?? Convert.DBNull;
query.Parameters["#AddressLine2"].Value = order.AddressLine2 ?? Convert.DBNull;
query.Parameters["#AddressLine3"].Value = order.AddressLine3 ?? Convert.DBNull;
query.Parameters["#City"].Value = order.City ?? Convert.DBNull;
query.Parameters["#StateOrRegion"].Value = order.StateOrRegion ?? Convert.DBNull;
query.Parameters["#PostalCode"].Value = order.PostalCode ?? Convert.DBNull;
query.Parameters["#Title"].Value = order.Title ?? Convert.DBNull;
query.Parameters["#ItemPrice"].Value = order.ItemPrice ?? Convert.DBNull;
query.Parameters["#ShippingPrice"].Value = order.ShippingPrice ?? Convert.DBNull;
query.Parameters["#Quantity"].Value = order.Quantity ?? Convert.DBNull;
query.ExecuteNonQuery();
transaction.Commit();
}
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}
}
You open and close the connection without actually executing anything.
Also, move the adding of the parameters outside of your foreach loop and just set the values within the loop.
using (SqlCommand query = new SqlCommand(customerInsert))
{
query.Connection = conn;
query.Parameters.Add("#AmazonOrderId", SqlDbType.NVarChar, 150);
query.Parameters.Add("#Name", SqlDbType.NVarChar, 150);
query.Parameters.Add("#AddressLine1", SqlDbType.NVarChar, 150);
query.Parameters.Add("#AddressLine2", SqlDbType.NVarChar, 150);
query.Parameters.Add("#AddressLine3", SqlDbType.NVarChar, 150);
query.Parameters.Add("#City", SqlDbType.NVarChar, 150);
query.Parameters.Add("#StateOrRegion", SqlDbType.NVarChar, 150);
query.Parameters.Add("#PostalCode", SqlDbType.NVarChar, 150);
query.Parameters.Add("#Title", SqlDbType.NVarChar, 150);
query.Parameters.Add("#ItemPrice", SqlDbType.NVarChar, 150);
query.Parameters.Add("#ShippingPrice", SqlDbType.NVarChar, 150);
query.Parameters.Add("#Quantity", SqlDbType.NVarChar, 150);
conn.Open();
foreach (var order in orders)
{
query.Parameters["#AmazonOrderId"].Value = order.AmazonOrderId;
query.Parameters["#Name"].Value = order.Name;
query.Parameters["#AddressLine1"].Value = order.AddressLine1;
query.Parameters["#AddressLine2"].Value = order.AddressLine2;
query.Parameters["#AddressLine3"].Value = order.AddressLine3;
query.Parameters["#City"].Value = order.City;
query.Parameters["#StateOrRegion"].Value = order.StateOrRegion;
query.Parameters["#PostalCode"].Value = order.PostalCode;
query.Parameters["#Title"].Value = order.Title;
query.Parameters["#ItemPrice"].Value = order.ItemPrice;
query.Parameters["#ShippingPrice"].Value = order.ShippingPrice;
query.Parameters["#Quantity"].Value = order.Quantity;
query.ExecuteNonQuery();
}
conn.Close();
}
Apart from the obvious error of the missing ExecuteNonQuery call, your code will fail again because you add the parameters at each loop. This will lead to an exception caused by a parameter already defined.
You need to clear the parameters collection with
cmd.Parameters.Clear();
at the start of each loop or better define the parameters before the loop and, inside the loop, change only the values
using (conn = new SqlConnection(connectionstring))
{
string customerInsert = #"INSERT INTO AmazonCustomer
(AddressLine1, AddressLine2, AddressLine3, City,
StateOrRegion, AmazonOrderId, PostalCode, Title,
ItemPrice, ShippingPrice, Quantity)
VALUES (#AddressLine1, #AddressLine2, #AddressLine3, #City,
#StateOrRegion, #AmazonOrderId, #PostalCode, #Title,
#ItemPrice, #ShippingPrice, #Quantity)";
using (SqlCommand query = new SqlCommand(customerInsert))
{
query.Connection = conn;
// Not used ???
// query.Parameters.Add("#Name", SqlDbType.NVarChar, 150)
query.Parameters.Add("#AddressLine1", SqlDbType.NVarChar, 150);
query.Parameters.Add("#AddressLine2", SqlDbType.NVarChar, 150);
query.Parameters.Add("#AddressLine3", SqlDbType.NVarChar, 150);
query.Parameters.Add("#City", SqlDbType.NVarChar, 150);
query.Parameters.Add("#StateOrRegion", SqlDbType.NVarChar, 150);
query.Parameters.Add("#AmazonOrderId", SqlDbType.NVarChar, 150);
query.Parameters.Add("#PostalCode", SqlDbType.NVarChar, 150);
query.Parameters.Add("#Title", SqlDbType.NVarChar, 150);
query.Parameters.Add("#ItemPrice", SqlDbType.NVarChar, 150);
query.Parameters.Add("#ShippingPrice", SqlDbType.NVarChar, 150);
query.Parameters.Add("#Quantity", SqlDbType.NVarChar, 150);
conn.Open();
using(SqlTransaction tr = conn.BeginTransaction())
{
foreach (var order in orders)
{
// Not used ???
// query.Parameters["#Name"].Value = order.Name;
query.Parameters["#AddressLine1"].Value = order.AddressLine1;
query.Parameters["#AddressLine2"].Value = order.AddressLine2;
query.Parameters["#AddressLine3"].Value = order.AddressLine3;
query.Parameters["#City"].Value = order.City;
query.Parameters["#StateOrRegion"].Value = order.StateOrRegion;
query.Parameters["#AmazonOrderId"].Value = order.AmazonOrderId;
query.Parameters["#PostalCode"].Value = order.PostalCode;
query.Parameters["#Title"].Value = order.Title;
query.Parameters["#ItemPrice"].Value = order.ItemPrice;
query.Parameters["#ShippingPrice"].Value = order.ShippingPrice;
query.Parameters["#Quantity"].Value = order.Quantity;
query.ExecuteNonQuery();
}
tr.Commit();
}
}
}
Notice that the connection could be opened at the start of the loop and not at each loop, while the using statement take cares to close and dispose it, and given the nature of your inserts, I suggest also to encapsulate everything inside a SqlTransaction so your code is more 'atomic'.
Finally something is not correct in your query, there is a parameter placeholder that has not matching parameter in the collection (#ID) and there is a parameter that has no placeholder in the query (#name). Without fixing these two errors you get other errors. I have removed them from the query and from the parameters collection
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da1 = new SqlDataAdapter();
DataSet ds1 = new DataSet();
cn.ConnectionString = #"Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True";
cn.Open();
string n = Convert.ToString(txtfid.Text);
cmd.CommandText = "select * from Personal_det where FID=" + n + "";
cmd.CommandType = CommandType.Text;
cmd.Connection = cn;
da1.SelectCommand = cmd;
da1.Fill(ds1, "Personal_det");
double dn = ds1.Tables["Personal_det"].Rows.Count;
if (dn == 0)
{
DateTime sdt = DateTime.Today;
SqlCommand cmd3 = new SqlCommand();
cn.Close();
cn.Open();
cmd3.CommandText = "insert into Personal_det(FID,Name,DOB,MobileNo,EmailId,add1,add2,add3,Pincode) values(#FID,#Name,#DOB,#MobileNo,#EmailId,#add1,#add2,#add3,#Pincode)";
cmd3.CommandType = CommandType.Text;
cmd3.Connection = cn;
cmd3.Parameters.Add("#FID", SqlDbType.VarChar,50);
cmd3.Parameters["#FID"].Value = this.txtfid.Text;
cmd3.Parameters.Add("#Name", SqlDbType.VarChar, 50);
cmd3.Parameters["#Name"].Value = this.txtname.Text;
cmd3.Parameters.Add("#DOB", SqlDbType.DateTime, 8);
cmd3.Parameters["#DOB"].Value = Convert.ToDateTime(this.txtdob.Text);
cmd3.Parameters.Add("#MobileNo", SqlDbType.Decimal, 18);
cmd3.Parameters["#MobileNo"].Value = this.txtmbl.Text;
cmd3.Parameters.Add("#EmailId", SqlDbType.VarChar, 50);
cmd3.Parameters["#EmailId"].Value = this.txtmail.Text;
cmd3.Parameters.Add("#add1", SqlDbType.VarChar, 50);
cmd3.Parameters["#add1"].Value = this.txtadd1.Text;
cmd3.Parameters.Add("#add2", SqlDbType.VarChar, 50);
cmd3.Parameters["#add2"].Value = this.txtadd2.Text;
cmd3.Parameters.Add("#add3", SqlDbType.VarChar, 50);
cmd3.Parameters["#add3"].Value = this.txtadd3.Text;
cmd3.Parameters.Add("#Pincode", SqlDbType.Decimal, 18);
cmd3.Parameters["#Pincode"].Value = this.txtpin.Text;
cmd3.ExecuteNonQuery();
cn.Close();
This is my C# code..Actually what i have done is previously i had FID as int now i converted it as varchar(50),because of some needs,i have already changed the datatype in sql..Personal_det is the table where in FID is a primary key constraint and foriegn key for other tables..Now When i am going to execute the code..it gives the error shown in the Image
Do not create CommandText using strings concatenation - such a bad practice make your sql queries vulnerable for sql-injections.
You can use SqlParameter also for "select" queries, like this.
cmd.CommandText = "select * from Personal_det where FID=#fid";
...
cmd.Parameters.Add(new SqlParameter("#fid", fidValue))
since FID is string you need to use '' as below
cmd.CommandText = "select * from Personal_det where FID='" + n + "'";
above will avoid your exception but it is not safe
You need to use parameters like you did on second case
I do not clearly understand how to format the SqlDataAdapter for output variables when working with C#
Error Message:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Code Example (Stored Procedure works fine):
private DataTable updateOrdEodHold(DataTable tb, out string mnpft,
out string authld, out string trd, out string hld, out string extnow)
{
// Start the connection string.
string connstr = ConfigurationManager.ConnectionStrings
["LocalSqlServer"].ConnectionString;
SqlConnection myConn = new SqlConnection(connstr);
// Declare symbol and assign for Errors Catch Exception.
string Symbol = "";
string sqlComm = "dbo.UpdateOrdEodHold";
DataTable HoldVals = new DataTable();
SqlDataAdapter dataAdp = new SqlDataAdapter(sqlComm, myConn);
dataAdp.SelectCommand.CommandType = CommandType.StoredProcedure;
string ticker = (string)Convert.ToString(tb.Rows[0]["Ticker"]);
// update Symbol for Catch ex
Symbol = ticker.ToString();
String company = (string)Convert.ToString(tb.Rows[0]["Company"]);
String avgprofit = (string)Convert.ToString(tb.Rows[0]["AvgProfit"]);
String extdte = (string)Convert.ToString(tb.Rows[0]["ExitDate"]);
dataAdp.SelectCommand.Parameters.Clear();
dataAdp.SelectCommand.Parameters.Add(new SqlParameter("#ticker",
SqlDbType.VarChar, 10));
dataAdp.SelectCommand.Parameters["#ticker"].Value =
(string)ticker.ToString();
dataAdp.SelectCommand.Parameters.Add(new SqlParameter("#company",
SqlDbType.VarChar, 25));
dataAdp.SelectCommand.Parameters["#company"].Value =
(string)company.ToString();
dataAdp.SelectCommand.Parameters.Add(new SqlParameter("#avgpft",
SqlDbType.VarChar, 10));
dataAdp.SelectCommand.Parameters["#avgpft"].Value =
(string)avgprofit.ToString();
dataAdp.SelectCommand.Parameters.Add(new SqlParameter("#mnpft",
SqlDbType.VarChar, 10));
dataAdp.SelectCommand.Parameters["#mnpft"].Direction =
ParameterDirection.Output;
dataAdp.SelectCommand.Parameters.Add(new SqlParameter("#authld",
SqlDbType.VarChar, 6));
dataAdp.SelectCommand.Parameters["#authld"].Direction =
ParameterDirection.Output;
dataAdp.SelectCommand.Parameters.Add(new SqlParameter("#hld",
SqlDbType.VarChar, 6));
dataAdp.SelectCommand.Parameters["#hld"].Direction =
ParameterDirection.Output;
dataAdp.SelectCommand.Parameters.Add(new SqlParameter("#trd",
SqlDbType.VarChar, 6));
dataAdp.SelectCommand.Parameters["#trd"].Direction =
ParameterDirection.Output;
dataAdp.SelectCommand.Parameters.Add(new SqlParameter("#extnow",
SqlDbType.VarChar, 6));
dataAdp.SelectCommand.Parameters["#extnow"].Direction =
ParameterDirection.Output;
dataAdp.SelectCommand.Parameters.Add(new SqlParameter("#extdte",
SqlDbType.VarChar, 15));
dataAdp.SelectCommand.Parameters["#extdte"].Value =
(string)extdte.ToString();
dataAdp.Fill(HoldVals);
mnpft = HoldVals.Rows[0]["MinProfit"].ToString();
authld = HoldVals.Rows[0]["AutoHold"].ToString();
trd = HoldVals.Rows[0]["Trade"].ToString();
hld = HoldVals.Rows[0]["Hold"].ToString();
extnow = HoldVals.Rows[0]["ExitNow"].ToString();
return HoldVals;
}
You need to hold a reference to the Output parameter variable so that you can access the value returned to it using parameter.Value once the adapter has executed the command.
//Create the parameter
SqlParameter parameter = new SqlParameter("#mnpft", SqlDbType.VarChar);
//Set the parameter direction as output
parameter.Direction = ParameterDirection.Output;
sqlCommand.Parameters.Add(parameter);
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
sqlAdapter.Fill(dataSet);
//Fetch the output parameter after doing the Fill
string outputValue = Convert.ToString(parameter.Value);