我正在尝试创建一个简单的WPF项目,该项目接收通过文本框输入的学生详细信息,然后在单击添加学生按钮后将它们存储到学生数据库中。
这个工作正常并且在数据库中存储没问题
private void OnAddNewStudent(object sender, RoutedEventArgs e)
{
using (db1104983Entities1 context = new db1104983Entities1()) //Contained within using so it automatically disposes when it is out of scope
{
Student student1 = new Student //creates new Student Object
{
MatricNo= txtM.Text,
FirstName = txt1.Text,
LastName = txt2.Text,
Component1 = txtcom1.Text,
Component2 = txtcom2.Text,
Component3 = txtcom3.Text,
};
MessageBox.Show("Student Added Succesfully"); // Advises user the record has succesfully been added
context.Students.Add(student1); //Adds Student object to DB
context.SaveChanges(); // Commits change to dDB
//Clears all textboxes once record has been added
txtM.Clear();
txt1.Clear();
txt2.Clear();
txtcom1.Clear();
txtcom2.Clear();
txtcom3.Clear();
}
}
我现在正在尝试使用拖动所需表格的数据绑定方法来显示数据库中的内容,在这种情况下,该表格是学生。在xaml中的网格上。
既然我已经这样做了,它就不会在网格中显示任何内容,它也不再符合并且告诉我我的matricno,firstName等之间存在歧义
见错误
我只是想检查一下我做错了是不是真的很明显?
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="517">
<TabItem Header="Input">
<Grid Background="#FFE5E5E5" Margin="0,-1,-14,-7">
<Button Content="Add Student" HorizontalAlignment="Left" Margin="10,252,0,0" VerticalAlignment="Top" Width="94" Height="26" Click="OnAddNewStudent"/>
<TextBox x:Name="txtM" HorizontalAlignment="Left" Height="23" Margin="148,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txtcom1" HorizontalAlignment="Left" Height="23" Margin="355,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txt1" HorizontalAlignment="Left" Height="23" Margin="148,75,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txtcom2" HorizontalAlignment="Left" Height="23" Margin="355,75,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txtcom3" HorizontalAlignment="Left" Height="23" Margin="355,128,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txt2" HorizontalAlignment="Left" Height="23" Margin="148,128,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label Content="First Name:" HorizontalAlignment="Left" Margin="11,75,0,0" VerticalAlignment="Top" Height="23" Width="93"/>
<Label Content="Matriculation No:" HorizontalAlignment="Left" Margin="11,22,0,0" VerticalAlignment="Top" Height="23" Width="105"/>
<Label Content="Last Name:" HorizontalAlignment="Left" Margin="11,128,0,0" VerticalAlignment="Top" Height="23" Width="93"/>
<Label Content="Com 1" HorizontalAlignment="Left" Margin="302,22,0,0" VerticalAlignment="Top"/>
<Label Content="Com 2" HorizontalAlignment="Left" Margin="302,72,0,0" VerticalAlignment="Top"/>
<Label Content="Com 3" HorizontalAlignment="Left" Margin="302,125,0,0" VerticalAlignment="Top"/>
<Button Content="Purge Database" HorizontalAlignment="Left" Margin="130,252,0,0" VerticalAlignment="Top" Width="94" Height="26" Click="OnPurgeDB"/>
</Grid>
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</Grid>
</Window>
答案 0 :(得分:1)
你需要查看你的xaml文件
此错误是由两个名称相同的项目引起的,删除或重命名其中一个
<Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="74.778,81.038,0,0" VerticalAlignment="Top"/>
<Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="83.781,135.057,0,0" VerticalAlignment="Top"/>
答案 1 :(得分:0)
由于您的学生课程是一个部分课程,正如我在截屏(http://i.imgur.com/crLefOp.png?1)中看到的那样,您可以查看包含该课程其他部分的文件,以获得另一个同名的声明。
答案 2 :(得分:0)
问题是因为当我拖放新数据网格时,它创建了另一个带有变量名称的文件,删除文件后修复了问题