if i need top 5 question we can write like this ....
SELECT top 5 questions FROM [tblques] ORDER BY Rnd(quescode)
I have combobox contains the values 10,20,50,100,
when i select 10 from the combobox ths should execute top 10 from table
if i select the value as 50 it should take and display 50 question
how to do ths in windows form c# using access database
Loading
VulpesPosted Jul 17, 2012, 9:35 AM
rashmi kcPosted Jul 17, 2012, 8:44 AM
if i want to execute for n number of value then we cant write the code
is it possible something like
SELECT top questions where quesnum ='" + combobox.text +" ' FROM [tblques] ORDER BY Rnd(quescode)
Satyapriya NayakPosted Jul 17, 2012, 8:26 AM
Try this...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Combobox
{
public partial class Form3 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
string str;
OleDbDataAdapter oledbda;
DataSet ds;
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Select");
comboBox1.Text = "Select";
comboBox1.Items.Add("1");
comboBox1.Items.Add("2");
comboBox1.Items.Add("3");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
if (comboBox1.SelectedItem.ToString() == "1")
{
con.Open();
str = "select top 1 * FROM subject";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "subject");
dataGridView1.DataMember = "subject";
dataGridView1.DataSource = ds;
con.Close();
}
else if (comboBox1.SelectedItem.ToString() == "2")
{
con.Open();
str = "select top 2 * FROM subject";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "subject");
dataGridView1.DataMember = "subject";
dataGridView1.DataSource = ds;
con.Close();
}
else if (comboBox1.SelectedItem.ToString() == "3")
{
con.Open();
str = "select top 3 * FROM subject";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "subject");
dataGridView1.DataMember = "subject";
dataGridView1.DataSource = ds;
con.Close();
}
else
{
dataGridView1.DataSource = null;
}
}
}
}
Thanks
If this post helps you mark it as answer