I try to develop a web based survey creation and filling app on ASP.NET and C# getting data from Sql Server. I use DataBind to bind my question names and answers. Below in the picture, I need to show only the question number (like 1, 2, 3 etc.) in RadioButtonList (not with the question name) and only the question name in a label lblQuestion as;
protected void rbtnQuestions_SelectedIndexChanged(object sender, EventArgs e)
{
General.myquestionid = System.Convert.ToInt32(rbtnQuestions.SelectedValue);
lblQuestion.Text = rbtnQuestions.SelectedItem.Text;
}
I also need the "id" of survey_question table to display the answers below the question since I use it in SelectedIndexChanged event.
But since I use DataTextField, both RadioButtonList text and lblQuestion.Text show the same thing. My way of binding the data is below:
string sqlStr = "SELECT id, question_no, question, survey_answer_type_id FROM survey_question WHERE survey_id = '" + General.mysurveyid + "'";
cmd = new SqlCommand(sqlStr, connection);
da.SelectCommand = cmd;
da.Fill(data);
rbtnQuestions.DataSource = data;
rbtnQuestions.DataTextField = "question";
rbtnQuestions.DataValueField = "id";
rbtnQuestions.DataBind();
cmd.Dispose();
How can I achieve this?
Thanks.
On Page load event Set
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sqlStr = "SELECT id,question_no, CONVERT(varchar(10),(ROW_NUMBER() over (order by question desc))) AS QueNo,question, survey_answer_type_id FROM survey_question WHERE survey_id = '" + General.mysurveyid + "'";
cmd = new SqlCommand(sqlStr, connection);
da.SelectCommand = cmd;
da.Fill(data);
ViewState["data"] = data;
rbtnQuestions.DataSource = data;
rbtnQuestions.DataTextField = "QueNo";
rbtnQuestions.DataValueField = "id";
rbtnQuestions.DataBind();
cmd.Dispose();
}
}
And Question Set On SelectedIndexChanged Event
protected void rbtnQuestions_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = (DataSet)ViewState["data"];
DataRow[] row = ds.Tables[0].Select("id = '" + rbtnQuestions.SelectedValue + "'");
lblQuestion.Text = row[0]["question"].ToString();
}
Related
I am writing a project in ASP.net which I have six text boxes to display the information of the next record in my database. And I also have a search button base on some selected field which will search out my first record in my database. I inserted the code for my first record into the search button event handler. The code is written below:
Using System.Data;
Using System.Data.OleDb;
Using System.IO;
OleDbconnection mycon = new OleDbconnection("");
protected void search_btn_Click(object sender, EventArgs e)
{
int i = 0;
DataTable dt;
String searchme = "select * from student where myclass = '" + txtclass.Text + "' and mytype = '" + txtclasstype.Text + "'";
OleDbCommand cmd = new OleDbCommand (searchme, mycon);
mycon.Open();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count > 0)
{
i = 0;
txtsearbox.Text = dt.Rows[i]["Admin_no"].ToString();
txtsurname.Text = dt.Rows[i]["surname"].ToString();
txtfirstname.Text = dt.Rows[i]["firstname"].ToString();
txtothername.Text = dt.Rows[i]["othername"].ToString();
txtsex.Text = dt.Rows[i]["sex"].ToString();
Photo.ImageUrl = dt.Rows[i]["photo"].ToString();
// then I created a session to hold my dt.
Session["dt"] = dt;
}
mycon.Close();
}
The above code is working fine for my first record but where I am having problem is from my next button which is written as shown below:
protected void next_btn_Click(object sender, EventArgs e)
{
int rowIndex = 0;
rowIndex ++;
if(rowIndex <= dt.Rows.Count)
{
txtsearbox.Text = dt.Rows[rowIndex]["Admin_no"].ToString();
txtsurname.Text = dt.Rows[rowIndex]["surname"].ToString();
txtfirstname.Text = dt.Rows[rowIndex]["firstname"].ToString();
txtothername.Text = dt.Rows[rowIndex]["othername"].ToString();
txtsex.Text = dt.Rows[rowIndex]["sex"].ToString();
Photo.ImageUrl = dt.Rows[rowIndex]["photo"].ToString();
}
mycon.Close();
}
The error it shows is: There is no row at position 1.
I have basically a combobox and a text box.
The combobox consists of subject code, whereas the textbox consists of a subject name which is stored in the database.
Is there any way that when an item from combobox is selected, it automatically displays the subject name based on the selected subject code, which will be from the database?
Below is my code, but it is not working.
private void Form1_Load(object sender, EventArgs e)
{
con.Open();
OleDbDataAdapter oda1 = new OleDbDataAdapter("select subject_code from subjectinfo", con);
DataTable dt1 = new DataTable();
oda1.Fill(dt1);
comboBoxSubjectCodeUpdate.DataSource = dt1;
comboBoxSubjectCodeUpdate.DisplayMember = "subject_code";
comboBoxSubjectCodeUpdate.SelectedIndex = -1;
con.Close();
}
private void comboBoxSubjectCodeUpdate_SelectedIndexChanged(object sender, EventArgs e)
{
string str = "select subject_abbreviation from subjectinfo where subject_code ='" + comboBoxSubjectCodeUpdate.Text + "'";
OleDbCommand cmd = new OleDbCommand(str, con);
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
textBox1.Text = dr["subject_abbreviation"].ToString();//column name should be that you want to show on textbox
}
}
I want to filter the GridView depend on the TextBox in form down .. I want when I write for example ec it shows all rows with company name include ec then if I change the string in the TextBox the data changes also in the GridView.
DoubleClick on your TextBox and write code here:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string sql = "SELECT * FROM Table WHERE Name LIKE #Name";
using (SqlCommand cmd= new SqlCommand(sql, cn))
{
cmd.Parameters.AddWithValue("#Name", "%" + textBox1.Text + "%");
//rest of the code
//dataGridView.DataSource = your DataSource;
}
}
Edit: To avoid pulling data from the db on each keystroke, Try this in textBox1_TextChanged Event:
DataSet ds = new DataSet();//Populate ds from a SqlDataAdapter
private void textBox1_TextChanged(object sender, EventArgs e)
{
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = string.Format("name LIKE '%{0}%'", textBox1.Text);
dataGridView1.DataSource = dv;
}
Have you tried the row filter property on the datagrid?
(dataGridViewFields.DataSource as DataTable).DefaultView.RowFilter = string.Format("Field = '{0}'", textBoxFilter.Text);
See Brad Bruce's answer here for an example.
using query i have called two columns value from database into one column. the point is now i want to select a value form combobox and put one column value into textbox.
e.g
two column values from database into combobox below
10001 haider <------ when i select this index i want only haider to be viewed into the textbox
10002 fahad
10003 aitazaz
the snippet which i have used for calling the two colums value from database is:
public void account()
{
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT acc_no, acc_name FROM accounts_record";
MySqlDataAdapter adpt = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
cbacc.Items.Add(ds.Tables[0].Rows[i][0] + " " + ds.Tables[0].Rows[i][1]);
}
con.Close();
}
You should be adding values and text to the combobox separately.
Here's an example ComboBox: Adding Text and Value to an Item (no Binding Source).
If you have to display the id in the text you have to do some parsing before putting the selected text into the textbox.
Use ComboBox.SelectedIndexChanged event of the combobox to populate the textbox accordingly. use http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v=vs.110).aspx
If you are able to get the 2 value text of the combo box on selection change then you can split it with space (" ") character and take the 2nd string and put it in textbox.
For example
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string[] splitedStr = comboBox1.SelectedText.Split(' ');
textBox1.Text = splitedStr[1];
}
You should use the code as below
private void Form1_Load(object sender, EventArgs e)
{
string conString = "Data Source=\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True";
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT acc_no +'-' + acc_name as AccNoWithName , acc_no as ActNo FROM accounts_record";
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet dsn = new DataSet();
adpt.Fill(dsn);
con.Close();
comboBox1.DisplayMember = "AccNoWithName";
comboBox1.ValueMember = "ActNo";
comboBox1.DataSource = dsn.Tables[0];
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = comboBox1.SelectedValue.ToString();
}
string Sql_type = "select property_type_id,type_name from lk_tb_property_type";
OleDbCommand cmd_type = new OleDbCommand(Sql_type, con);
OleDbDataReader DR_two = cmd_type.ExecuteReader();
DataTable table_two = new DataTable();
table_two.Load(DR_two);
//begin adding line
DataRow row_two = table_two.NewRow();
row_two["type_name"] = "Select Poperty Name";
row_two["property_type_id"] = 0;
table_two.Rows.InsertAt(row_two, 0);
//end adding a line
combo_type.DataSource = table_two;
combo_type.DisplayMember = "type_name";
combo_type.ValueMember = "property_type_id";
combo_type.Text = "Select Poperty Name";
with this code i am fetching values for a combobox from database.now suppose my combobx is having 2 items named A and B..I have one more combobox...now what i want is that when user chooses item A from combobox the second combobox should display data related to item A when user chooses item B then data related to item B should be displayed...sohow to achieve this...??
you can fetch the data and bind it to combobox2 on SelectedIndexChanged event of combobox1
private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
var val = combobox1.SelectedValue;
// fetch data from database
// you need to set SQL parameter value form SelectedValue
combobox2.DataSource = ...; // set this value
combobox2.DisplayMember = .....; // set this value
combobox2.ValueMember = ....; // set this value
}
Please assign an event SelectedIndexChanged and AutoPostBack = true is this is a web Application in C#
In SelectedIndexChanged of combo box write your code. and make AutoPostBack = true of your combobox
You can do like these steps.
First, bind data to comboBox1 (I suppose that your first ComboBox named "comboBox1", and your form named "Form1"), please make sure that your SQL query command is correct for comboBox1
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(constr);
con.Open();
string Sql_cust_name = "select customer_name from tb_customer";
OleDbCommand cmd_cust_name = new OleDbCommand(Sql_cust_name, con);
OleDbDataReader DR_cust_name = cmd_cust_name.ExecuteReader();
DataTable table_cust_name = new DataTable();
table_cust_name.Load(DR_cust_name);
DataRow row_cust_name = table_cust_name.NewRow();
row_cust_name["customer_name"] = "Select Customer Name";
table_cust_name.Rows.InsertAt(row_cust_name, 0);
combo_cust_name.DataSource = table_cust_name;
combo_cust_name.DisplayMember = "customer_name";
combo_cust_name.ValueMember = "customer_name";
combo_cust_name.Text = "Select Customer Name";
con.Close();
}
Next, bind data to comboBox2 (I suppose that your second ComboBox named "comboBox2"), you have to get comboBox1.SelectedValue whenever it is changed, this value will be used in the filtering for data in comboBox2, so you have to handle SelectedIndexChanged event for comboBox1, please make sure that you have this code somewhere in your project: this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
To bind data to comboBox2 (I suppose that your second ComboBox named "comboBox2"), you have to get comboBox1.SelectedValue whenever it is changed
private void combo_cust_name_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(constr);
con.Open();
string customerName = "";
if (combo_cust_name.SelectedValue.GetType() == typeof(DataRowView))
{
DataRowView selectedRow = (DataRowView)combo_cust_name.SelectedValue;
customerName = selectedRow["customer_name"].ToString();
}
else
{
customerName = combo_cust_name.SelectedValue.ToString();
}
string Sql2 = "SELECT customer_number FROM tb_customer WHERE customer_name = '" + customerName + "'";
OleDbCommand cmd_type = new OleDbCommand(Sql2, con);
OleDbDataReader DR_two = cmd_type.ExecuteReader();
DataTable table_two = new DataTable();
table_two.Load(DR_two);
DataRow row_two = table_two.NewRow();
row_two["customer_number"] = "Select Customer Number";
table_two.Rows.InsertAt(row_two, 0);
comboBox2.DataSource = table_two;
comboBox2.DisplayMember = "customer_number";
comboBox2.ValueMember = "customer_number";
comboBox2.Text = "Select Customer Number";
}
Please correct the SQL query command as you want, but don't forget to put a correct filter like my sample code above.