Click here to Skip to main content
16,016,783 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,
I am learning sql server 2008 r2 and windows form c# in visual studio 2013. i want to write an application with fields like name, address, age, state, sex,date of birth etc. The application will have save,new, delete, update,search buttons etc. Can anybody help me with an article or url that uses sql server 2008 r2 and windows form c# in visual studio 2013 to achieve this?
Thanks in advance

What I have tried:

I have installed sql server 2008 r2 and visual studio 2013 ultimate on my machine. Also,I have downloaded some related articles but version problems have been frustrating me to kick start
Posted
Updated 11-Apr-17 10:09am
Comments
F-ES Sitecore 11-Apr-17 13:55pm    
You can't learn how to code from a forum, get a book on c# and go through it, most will cover forms applications and using SQL Server.
[no name] 11-Apr-17 14:00pm    
Google would find plenty of examples for you to look at.
Aydotcom 14-Apr-17 17:06pm    
Hi,
After studying the articles in the post i practiced using the following codes and also created a database sample as well as a table called tbl_records but i encountered an error " A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)" during compilation . Can somebody help me please?

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace InsertUpdateDeleteDemo
{
public partial class frmMain : Form
{
SqlConnection con = new SqlConnection("Data Source=akintomide-pc;Initial Catalog=Sample;Integrated Security=true;");
SqlCommand cmd;
SqlDataAdapter adapt;
//ID variable used in Updating and Deleting Record
int ID = 0;
public frmMain()
{
InitializeComponent();
DisplayData();
}

private void btn_Delete_Click(object sender, EventArgs e)
{
if (ID != 0)
{
cmd = new SqlCommand("delete tbl_Records where ID=@id", con);
con.Open();
cmd.Parameters.AddWithValue("@id", ID);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Deleted Successfully!");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Delete");
}
}

private void btn_Insert_Click_1(object sender, EventArgs e)
{
if (txt_Name.Text != "" && txt_State.Text != "")
{
cmd = new SqlCommand("insert into tbl_Records(Name,State) values(@name,@state)", con);
con.Open();
cmd.Parameters.AddWithValue("@name", txt_Name.Text);
cmd.Parameters.AddWithValue("@state", txt_State.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Inserted Successfully");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Provide Details!");
}
}
//Display Data in DataGridView
private void DisplayData()
{
con.Open();
DataTable dt = new DataTable();
adapt = new SqlDataAdapter("select * from tbl_Records", con);
adapt.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
//Clear Data
private void ClearData()
{
txt_Name.Text = "";
txt_State.Text = "";
ID = 0;
}

//Update Record
private void btn_Update_Click_1(object sender, EventArgs e)
{
if (txt_Name.Text != "" && txt_State.Text != "")
{
cmd = new SqlCommand("update tbl_Records set Name=@name,State=@state where ID=@id", con);
con.Open();
cmd.Parameters.AddWithValue("@id", ID);
cmd.Parameters.AddWithValue("@name", txt_Name.Text);
cmd.Parameters.AddWithValue("@state", txt_State.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Updated Successfully");
con.Close();
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Update");
}
}

private void dataGridView1_CellContentClick(obj

Here is an article that discusses ways to connect, which is the first thing you should know: Working with SQL Server Logins[^]
To keep things simple I would recommend starting with "Windows authentication".

If you need example connection strings, take a look here: SQL Server 2008 connection strings - ConnectionStrings.com[^]

The DotNetPerls site provides some good starting tutorials, look at: [www.dotnetperls.com/sqlclient]

Here is an interesting article which I found under "top articles" in CodeProject:
SQL Server Database Development in Visual Studio[^]
Sadly the author does not mention that depending on your version of VS 2013 you may need to install SSDT tooling for VS 2013.
Also this article does not show how to work with the database you created.
But nevertheless it shows some interesting techniques.

Of course there are lots of ways to work with SQL Server from VS 2013, good luck !
 
Share this answer
 
v3
Comments
Aydotcom 12-Apr-17 13:34pm    
The solutions really help me to kick start, thanks in millions.
RickZeeland 13-Apr-17 5:18am    
I tried to create a "SQL Server" project in VS2013 with SQL Server 2008 R2, but could not get it working. Maybe because I use the VS2013 Community Edition and SQL Express ? For now I just do everything in code, my experiences with VS generated adapters and database code are very disappointing !
Go through below article link

Click here
[^]

Set database and form field as per your requirement.

and let me know if you face any difficulties.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900