I have a SQL command:
string keyProcesses = "SELECT distinct STUFF((SELECT ', '+ cn.name from WMCCMCategories cn INNER JOIN CategorySets uc ON uc.categoryId = cn.categoryID INNER JOIN KeyProcesses u ON u.categorySetId = uc.setId INNER JOIN Companies c ON c.companyId = u.companyId WHERE c.companyName = @companyName ORDER BY cn.name FOR XML PATH('')), 1, 1, '') AS listStr FROM WMCCMCategories cnn Group by cnn.name";
And I have:
public SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = commandText; //where commandText is another SQL statement
sqlCmd.Parameters.Clear();
Then I execute connection:
string connectionString = ConfigurationSettings.AppSettings["connectionString"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
try
{
connection.Open();
SqlDataAdapter sdr = new SqlDataAdapter(sqlCmd.CommandText, connection);
DataTable dt=new DataTable();
sdr.Fill(dt)
After that I want to move to the next SQL command and add parameter to that command:
sqlCmd.CommandText = keyProcesses;
sqlCmd.Parameters.AddWithValue("@companyName", compnyName);
SqlDataAdapter sdr1 = new SqlDataAdapter(sqlCmd.CommandText, connection);
DataTable dtb1 = new DataTable();
sdr1.Fill(dtb1);
But it fail to execute this SqlCommand. I already make sure that the SQL key process can run properly with a fix parameter pre-added in. The compnName is not empty. The only suspect here is how to add value to sql parameter. I don't know what is wrong in my way of using it ?
Edit: I update the whole part of code for the easy of investigating the issue:
string connectionString = ConfigurationSettings.AppSettings["connectionString"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
try
{
connection.Open();
SqlDataAdapter sdr = new SqlDataAdapter(sqlCmd.CommandText, connection);
DataTable dt=new DataTable();
sdr.Fill(dt);
//To store the values of company names:
List<CompanyModel> companies = new List<CompanyModel>();
for(int i = 0; i < dt.Rows.Count; i++)
{
companies.Add(new CompanyModel
{
compnSN = i + 1,
compnName = dt.Rows[i]["companyName"].ToString(),
compnAddress = dt.Rows[i]["address"].ToString()
});
}
companyRepeater.DataSource = companies;
companyRepeater.DataBind();
string comName = dt.Rows[0]["companyName"].ToString();
var names = companies.Select(c => c.compnName);
string[] arr = names.ToArray();
foreach (string compnyName in arr)
{
//To write names to file for debugging purpose
using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/File.txt"), true))
{
_testData.WriteLine(compnyName); // Write the file.
}
//Get KeyProcesses
sqlCmd.CommandText = keyProcesses;
//sqlCmd.Parameters.AddWithValue("@companyName", compnyName);
sqlCmd.Parameters.Add("@companyName", SqlDbType.NVarChar);
sqlCmd.Parameters["@companyName"].Value = compnyName;
// SqlDataAdapter sdr1 = new SqlDataAdapter(sqlCmd.CommandText, connection);
DataTable dtb1 = new DataTable();
//sdr1.Fill(dtb1);
using (var reader = sqlCmd.ExecuteReader())
{
dtb1.Load(reader);
}
companies.Add(new CompanyModel
{
compnKeyProcesses = dtb1.Rows[0][0].ToString()
});
sqlCmd.Parameters.AddWithValue("@companyName", compnyName);DataTableis of little benefit here.DataTablewas useful for migrating from ADO.NET record sets; however, that was 10 years ago. It should not be your default data tool (except for a few scenarios involving unknown schemas). I would strongly suggest moving to a more object-based approach for data.SqlCommand commandis just a typo? It should beSqlCommand sqlCmd