Step by step Perform CRUD Operations in WPF with the use of Sql server
Through-out the article we will learn step-by-step: How to perform crud operations in WPF . We will discuss crud operations with usage of WPF. I have mention the code for Save,Edit,delete given below.
Abstract
Through-out the article we will learn step-by-step: How to perform crud operations in WPF. We will discuss crud operations with usage of WPF Introduction
In these days, WPF is most growing technology and as a developer, I am curious to know and learn about the new features of this framework.
From last couple of days I was wondering how can I perform CRUD operations using sql queries. For the same I have gone through various forums etc. Unfortunately, I did saw very few posts/articles for the same.
Finally, I tried and came with a solution to perform CRUD operations without the use of WPF. In this article, I am going to share my findings with all of you.Why Step-by-Step
Ah! This question came to my mind while I was finding the solution of my problem, I jotted down few points and finally called them as a steps to perform CRUD Operations.Pre-requisite
To implement the solution and to feel the taste of code, you should:What we are waiting for?
So, what we are waiting for here, lets get started. Just following these step(s) and we will done!Step1: Create WPF Project
Launch your visual studio and perform following action (Refer to snapshot)
File->New->Select the new project ->Wpf Application->Type the Project Name
Selecting a template for project:
Step2:Create Form and view
Right click the Project then add->New Item -> Select Window(WPF) give the form name, then drag and drop controls placed in the forms and double click the buttonStep3:Bind the Records using
In your view, write or copy/paste code, mentioned in following code-snippet:
private void FillGrid()
{
lstemp = GetDatas();
Dtgrid1.ItemsSource = lstemp;
}
Following code invoke the Insert and Update Records to database.
private void Cmd_Save_Click(object sender, RoutedEventArgs e)
{
if (Cmd_Save.Content == "Update")
{
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("Update TblEmp set Empno='" + TxtEmpNo.Text + "',EmpName='" + TxtName.Text + "' where id=" + editid, sqlcon);
sqlcmd.ExecuteNonQuery();
}
else if (Cmd_Save.Content == "Save")
{
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("Insert into TblEmp values('" + TxtEmpNo.Text + "','" + TxtName.Text + "')", sqlcon);
sqlcmd.ExecuteNonQuery();
}
else if (Cmd_Save.Content == "Delete")
{
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("Delete From TblEmp where id="+editid, sqlcon);
sqlcmd.ExecuteNonQuery();
}
FillGrid();
}
Following code-snippet defines How to select corresponding record and then to populate them.
private void Dtgrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
object item = Dtgrid1.SelectedItem;
DataTable dt1 = new DataTable();
string ID = (Dtgrid1.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
editid = Convert.ToInt32(ID);
sqlcon.Open();
SqlDataAdapter sqladpsc = new SqlDataAdapter("Select * from TblEmp where id=" + ID, sqlcon);
sqladpsc.Fill(dt1);
if (dt1.Rows.Count > 0)
{
TxtEmpNo.Text =dt1.DefaultView[0]["EmpNo"].ToString();
TxtName.Text =dt1.DefaultView[0]["EmpName"].ToString();
Cmd_Save.Content = "Update";
}
sqlcon.Close();
}
Now, Fetch the records from database by passing parameters and without parameters.
public List
{
sqladp = new SqlDataAdapter("Select * from TblEmp", sqlcon);
sqladp.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
lstemp.Add(new Employee() { EmpId = int.Parse(dr[0].ToString()), EmpNo = dr[1].ToString(), EmpName = dr[2].ToString()});
}
return lstemp;
}
public Employee GetDatasval(string Sqry)
{
sqladp = new SqlDataAdapter(Sqry, sqlcon);
sqladp.Fill(dt);
empcls.EmpId = Convert.ToInt32(dt.DefaultView[0][0]);
empcls.EmpName = dt.DefaultView[0][1].ToString();
empcls.EmpNo = dt.DefaultView[0][2].ToString();
return empcls;
}Creating Form
Write or copy/paste code to your MainWindow.xaml , mentioned in the following code-snippet:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="525" WindowStartupLocation="CenterScreen">
<Grid>
<Label Content="Name" HorizontalAlignment="Left" Margin="168,39,0,0" VerticalAlignment="Top"/>
<TextBox Name="TxtName" HorizontalAlignment="Left" Height="23" Margin="255,39,0,0" TextWrapping="Wrap" Text="" TabIndex="1" VerticalAlignment="Top" Width="120"/>
<Label Content="EmpNo" HorizontalAlignment="Left" Margin="168,82,0,0" VerticalAlignment="Top"/>
<TextBox Name="TxtEmpNo" HorizontalAlignment="Left" Height="23" Margin="255,82,0,0" TextWrapping="Wrap" Text="" TabIndex="2" VerticalAlignment="Top" Width="120"/>
<Button Content="Save" Name="Cmd_Save" HorizontalAlignment="Left" Margin="255,125,0,0" VerticalAlignment="Top" Width="101" Height="41" Click="Cmd_Save_Click"/>
<Label Content="Crud operations in WPF" HorizontalAlignment="Left" Margin="191,8,0,0" VerticalAlignment="Top" FontFamily="Arial Black" FontSize="14"/>
<DataGrid HorizontalAlignment="Left" Margin="10,192,0,0" VerticalAlignment="Top" Height="268" Width="497" Name="Dtgrid1" SelectionChanged="Dtgrid1_SelectionChanged"/>
</Grid>
</Window>
In this whole article, we discussed about CRUD operations using WPF. Conclusion
In this whole article, we discussed about CRUD operations using WPF.
1. Create a new project
2. Choose correct Project Type
3. Add New xaml Form
4. Add Methods using CommandButton
5. Add various operations
Hope, you find this article helpful.
Good start! Keep posting good contents.