Click here to Skip to main content
15,437,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
int a = Convert.ToInt32(Settings.Default["monocode"]);
this is the number limit .
i want to fetch this number of rows to listbox .

What I have tried:

int @a = Convert.ToInt32(Settings.Default["monocode"]);
                string query = " select top(@a)  MonoCode,MonoCode_Result,MasterCode,OuterCode,PalletCode from BatchDetails Where MonoCode_Result = 1 and MasterCode IS NULL " +
                    "and OuterCode IS NULL and PalletCode IS NULL ";
Posted
Updated 29-May-21 7:24am
Comments
Dave Kreskowiak 29-May-21 10:24am    
...and the question or problem would be .... ?
Member 15212425 29-May-21 10:28am    
Im not getting desired answer
Richard MacCutchan 29-May-21 10:24am    
See Solution below.
Member 15212425 29-May-21 10:31am     CRLF
I tried many methods ..this is one .. in sql they are showing this method ..thatz why i tried. can u plz help me
RedDk 29-May-21 11:54am    
intz R us, like right? First off ... correct your grammar. For instance "i" at the beginning of a sentence, should be "I". Then we'll see about some help.

You cannot create a string that way, you need to use a formatting method to create it. Something like:
C#
int rowcount = Convert.ToInt32(Settings.Default["monocode"]);
string query = string.Format($" select top({rowcount})  MonoCode,MonoCode_Result,MasterCode,OuterCode,PalletCode from BatchDetails Where MonoCode_Result = 1 and MasterCode IS NULL " +
                    "and OuterCode IS NULL and PalletCode IS NULL ");

And I recommend a more meaningful name for the count variable.
 
Share this answer
 
Comments
Member 15212425 30-May-21 0:53am     CRLF
Thank you sir .when im doing this . i got it . but top itmes are same data . what if i need to take single data . if data is like 2,3,3,4,4,5,5,6 .. i want to take data as 2,3,4,5 so on . i dont want duplication in my listbox
Richard MacCutchan 30-May-21 3:11am    
Then you need to use some other option to filter the results, maybe SQL SELECT DISTINCT Statement[^]. Seriously, spend some time actually learning the system you are trying to use.
In order for the TOP to work, you need to set the value for the bind variable before executing the statement. Consider the following example
C#
DataTable resultTable = new DataTable();

using (SqlConnection connection = new SqlConnection("Data Source=(local); Initial Catalog=master;Integrated Security=SSPI")) {
   using (SqlCommand command = new SqlCommand()) {
      command.CommandText = "SELECT TOP (@rowcount) * FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME";
      command.Connection = connection;
      command.Parameters.AddWithValue("@rowcount", 2);
      using (SqlDataAdapter adapter = new SqlDataAdapter(command)) {
         connection.Open();
         adapter.Fill(resultTable);
         connection.Close();
      }
   }
}

If the result is still not what expected, check if conditions in the WHERE clause are eliminating rows.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900