I am retrieving data from table Technology
in dropdownlist ddlTechnology
.
I have TechnologyId
as primary key in the table and values are 1,2,3,4
Now as per the technology,i have to add questions in question bank. but when i select any item in dropdownlist, my SelectedIndex
is always 0.
i want TechnologyId
from dropdownlist.
i have tried following code but its not working
using (dbDataContext dt = new dbDataContext())
{
var qry = from i in dt.Technologies
select i;
ddlTechnology.DataSource = qry;
ddlTechnology.DataValueField = "TechnologyId";
ddlTechnology.DataTextField = "TechnologyName";
ddlTechnology.DataBind();
ddlTechnology.Items.Insert(0, new ListItem("Select Technology", ""));
}
Add button to add question according to selected technology.
protected void btnAdd_Click(object sender, EventArgs e)
{
using (dbDataContext dt = new dbDataContext())
{
Question objQtn = new Question();
objQtn.Question1 = txtQuestion.Text;
objQtn.Option1 = txtOption1.Text;
objQtn.Option2 = txtOption2.Text;
objQtn.Option3 = txtOption3.Text;
objQtn.Answer = txtAnswer.Text;
// below here selectedIndex is always zero..
objQtn.TechnologyId = ddlTechnology.SelectedIndex;
dt.Questions.InsertOnSubmit(objQtn);
dt.SubmitChanges();
txtAnswer.Text = "";
txtOption1.Text = "";
txtOption2.Text = "";
txtOption3.Text = "";
txtQuestion.Text = "";
}
}