1

My code is as follows.

SqlCommand command = new SqlCommand();
command.Parameters.AddWithValue("@AccountId", accountNumberLong);
StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT * FROM T_POSTAGE_DISCOUNT");
sqlQuery = sql.ToString();

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    command.Connection = connection;
    command.CommandType = CommandType.Text;
    command.CommandText = sqlQuery;
    SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
    dataAdapter.Fill(dsDiscounts, "Discounts");
}

upto here its working fine. I want to add another table to this dataset. Then I did like this.

command.Parameters.AddWithValue("@DISCOUNT_LEVEL_NBR",discountLevelNBR);
sql.AppendLine("select * from T_CONTRACT WHERE DISCOUNT_LEVEL_NBR=@DISCOUNT_LEVEL_NBR");
sqlQuery = sql.ToString();
using (SqlConnection connection = new SqlConnection(connectionString))
{

    connection.Open();
    command.Connection = connection;
    command.CommandType = CommandType.Text;
    command.CommandText = sqlQuery;
    SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
    dataAdapter.Fill(dsDiscounts,"CONTRACT");

}

Its adding all the tables again. I want to add onlt contract table to existing datatset.
How can I do that?(c#)

sindhu jampani
  • 504
  • 3
  • 10
  • 30
  • I think you are appending the query again "sql.AppendLine("select * from T_CONTRACT WHERE DISCOUNT_LEVEL_NBR=@DISCOUNT_LEVEL_NBR");", so when you are executing for the second time it executes both... – Gopesh Sharma May 14 '14 at 06:41
  • Actually its a seperate query.Table names are different in both the queries.What should i do now? – sindhu jampani May 14 '14 at 06:43
  • 1
    create a new string builder StringBuilder sql1 = new StringBuilder(); and then sql1.AppendLine("select * from T_CONTRACT WHERE DISCOUNT_LEVEL_NBR=@DISCOUNT_LEVEL_NBR"); sqlQuery = sql1.ToString(); – Gopesh Sharma May 14 '14 at 06:46
  • Check out this [link](http://stackoverflow.com/q/11345761/3030434) – Amarnath Balasubramanian May 14 '14 at 06:54

2 Answers2

1

Try This.

DataTable myTable = new DataTable("MyTable");
adapter.Fill(myTable);
ds.Tables.Add(myTable);
cracker
  • 4,900
  • 3
  • 23
  • 41
0

The problem is from appending line you done it two time on string sql, when you write sql.AppendLine("..."); the string sql = "select * from T_CONTRACT WHERE DISCOUNT_LEVEL_NBR=@DISCOUNT_LEVEL_NBR".

Then in other line you add a command to string sql again so it become
sql = "select * from T_CONTRACT WHERE DISCOUNT_LEVEL_NBR=@DISCOUNT_LEVEL_NBR" "select * from T_CONTRACT WHERE DISCOUNT_LEVEL_NBR=@DISCOUNT_LEVEL_NBR"
So it is clear why it brings all table again.
So it is good to use two string sql1 , sql2.

Vencovsky
  • 28,550
  • 17
  • 109
  • 176