C# 在列表框中选择项时尝试显示数据集中所有行的值

C# 在列表框中选择项时尝试显示数据集中所有行的值,c#,sql,winforms,C#,Sql,Winforms,我试图能够单击列表框中的一个项目,并能够查看其对应行中的所有数据。想知道我的代码现在出了什么问题 我已多次尝试使用特定的选定列表框项查询数据库。但是它没有转换成字符串,它只显示“System.Data.Datarow”,因此,我无法成功地查询。原始查询可以工作,如果objects字段与行匹配,它将显示我要查找的正确姓氏。问题更多的是尝试单击列表框中的多个姓氏中的任意一个,然后让这些姓氏填充列表框旁边的空文本框 using System; using System.Collections.Gene

我试图能够单击列表框中的一个项目,并能够查看其对应行中的所有数据。想知道我的代码现在出了什么问题

我已多次尝试使用特定的选定列表框项查询数据库。但是它没有转换成字符串,它只显示“System.Data.Datarow”,因此,我无法成功地查询。原始查询可以工作,如果objects字段与行匹配,它将显示我要查找的正确姓氏。问题更多的是尝试单击列表框中的多个姓氏中的任意一个,然后让这些姓氏填充列表框旁边的空文本框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace College_Dorm_Mate_Matching_App
{
    public partial class Preferences : Form 
    {
        public Preferences()
        {
            InitializeComponent();
        }

        //create Student list and instance of student object
        List<Student> newStudent = new List<Student>();
        Student myStudent = new Student();
        SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = D:\CCSU\Spring 19\MIS  312\Final Project\College Dorm Mate Matching App\College Dorm Mate Matching App\Students.mdf; Integrated Security = True");

        //add textbox comobox values to student object
        public void AddItemsToObj()
        {
            myStudent.FirstName = textBoxFirstName.Text;
            myStudent.LastName = textBoxLastName.Text;
            myStudent.Age = int.Parse(textBoxAge.Text);
            myStudent.Sport = comboBoxSport.Text;
            myStudent.Music = comboBoxMusic.Text;
            myStudent.Hobbies = comboBoxHobbies.Text;
            myStudent.Recharge = comboBoxRecharge.Text;
            myStudent.Vices = comboBoxVices.Text;
            myStudent.Weekend = comboBoxWeekends.Text; 
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            AddItemsToObj();

            //query db
            SqlDataAdapter adapter = new SqlDataAdapter("Select * From  Students where FavoriteSport='"+ myStudent.Sport +"' AND Hobbies='"+ myStudent.Hobbies +"' AND MusicTaste='" + myStudent.Music+"'" , con);
            //create new table to store
            DataTable table = new DataTable();
            //fill new DT
            adapter.Fill(table);
            //add sources into listbox if they match
            listBoxMatches.DataSource = table;
            listBoxMatches.DisplayMember = "LastName";
            listBoxMatches.ValueMember = "FirstName";
        }

        private void listBoxMatches_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM Students where LastName='" + listBoxMatches.SelectedItem.ToString() + "'";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(dt);
            foreach(DataRow dr in dt.Rows)
            {
                textBoxfnameout.Text = dr["FirstName"].ToString();
                textBoxlnameout.Text = dr["LastName"].ToString();
                textBoxageout.Text = dr["Age"].ToString();
                textBoxsportout.Text = dr["FavoriteSport"].ToString();
                textBoxmusicout.Text = dr["MusicTaste"].ToString();
                textBoxhobbiesout.Text = dr["Hobbies"].ToString();
                textBoxrechargeout.Text = dr["Recharge"].ToString();
                textBoxweekendout.Text = dr["Weekend"].ToString();
                textBoxcigout.Text = dr["cigs"].ToString();
            }
            con.Close();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Data.SqlClient;
名称空间大学宿舍伴侣匹配应用程序
{
公共部分类首选项:表单
{
公众偏好()
{
初始化组件();
}
//创建学生列表和学生对象的实例
List newStudent=新列表();
学生myStudent=新学生();
SqlConnection con=new SqlConnection(@“数据源=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\CCSU\Spring 19\MIS 312\Final Project\College dom Mate Mate match App\College dom Mate match App\Students.mdf;集成安全性=True”);
//将文本框值添加到学生对象
公共void additemstobj()
{
myStudent.FirstName=textBoxFirstName.Text;
myStudent.LastName=textBoxLastName.Text;
myStudent.Age=int.Parse(textBoxAge.Text);
myStudent.Sport=comboBoxSport.Text;
myStudent.Music=comboBoxMusic.Text;
myStudent.cabiods=comboboxcabiods.Text;
myStudent.Recharge=comboBoxRecharge.Text;
myStudent.Vices=comboBoxVices.Text;
myStudent.Weekend=comboBoxWeekends.Text;
}
私有void btnSubmit\u单击(对象发送者,事件参数e)
{
additemstobj();
//查询数据库
SqlDataAdapter=new SqlDataAdapter(“从学生中选择*,其中FavoriteSport='”+myStudent.Sport+“'和嗜好='”+myStudent.cabiods+“'和MusicTaste='”+myStudent.Music+”,con);
//创建要存储的新表
DataTable=新的DataTable();
//填写新的DT
适配器。填充(表格);
//如果源匹配,则将其添加到列表框中
listBoxMatches.DataSource=表;
listBoxMatches.DisplayMember=“LastName”;
listBoxMatches.ValueMember=“FirstName”;
}
private void listBoxMatches\u SelectedIndexChanged(对象发送方,事件参数e)
{
con.Open();
SqlCommand cmd=con.CreateCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=“SELECT*FROM student where LastName=”+listBoxMatches.SelectedItem.ToString()+”;
cmd.ExecuteNonQuery();
DataTable dt=新的DataTable();
SqlDataAdapter sda=新的SqlDataAdapter(cmd);
sda.填充(dt);
foreach(数据行dr在dt.行中)
{
textBoxfnameout.Text=dr[“FirstName”].ToString();
textBoxlnameout.Text=dr[“LastName”].ToString();
textBoxageout.Text=dr[“Age”].ToString();
textBoxsportout.Text=dr[“FavoriteSport”].ToString();
textBoxmusicout.Text=dr[“musictast”].ToString();
textBoxhobbiesout.Text=dr[“嗜好”].ToString();
textBoxrechargeout.Text=dr[“Recharge”].ToString();
textBoxweekendout.Text=dr[“weeken”].ToString();
textBoxcigout.Text=dr[“cigs”].ToString();
}
con.Close();
}
}
}

我希望在列表框之外的文本框中填充selecteditem的相应数据。

请确保您阅读了标签信息,以免无意中误用它们。
visual studio
标记用于询问有关visual studio应用程序的问题(与使用它编写的代码相反)。请确保阅读标记的信息,以免无意中误用它们。
visualstudio
标记用于询问有关visualstudio应用程序的问题(而不是使用它编写的代码)。