Arun Kumar

Arun Kumar

  • NA
  • 35
  • 12.4k

Need Key,Value for get Data

Nov 6 2015 12:17 AM
Hi every one,
I am developing in windows forms.
//Retriving Student name from Regisrtation based on class
public void Autocomplete()
{
try
{
conn.Open();
string ct = "select RTRIM(name) from Stu_Restration where Current_Class = '" + CmbClass.Text + "'";
cmd = new SqlCommand(ct);
cmd.Connection = conn;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
cmbStudentName.Items.Add(rdr[0]);
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I Am using this code for retrive students. if multiple students have same name, in this scenario am not
getting values of second student.
For this i wrote below code, bt its giving an error.
class Language
{
public string Name { get; set; }
public string Code { get; set; }
}
public void Autocomplete1()
{
try
{
conn.Open();
string ct = "select RTRIM(name),RTRIM(Admission_No) from Stu_Restration where Current_Class = '" + CmbClass.Text + "'";
cmd = new SqlCommand(ct);
cmd.Connection = conn;
rdr = cmd.ExecuteReader();
Language lan;
List<Language> languages = new List<Language>();
while (rdr.Read())
{
lan = new Language();
lan.Name = rdr[0].ToString();
string Stu_Name = rdr[0].ToString();
lan.Code = rdr[1].ToString();
string ID = rdr[1].ToString();
languages.Add(lan);
List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();
// Add data to the List
data.Add(new KeyValuePair<string, string>(Stu_Name, ID));
cmbStudentName.DataSource = new BindingSource(data, null);
cmbStudentName.DisplayMember = "Value";
cmbStudentName.ValueMember = "Key";
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

Answers (3)