using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DataClerk
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtPNo.Clear();
txtPName.Clear();
txtSiteName.Clear();
txtJCNo.Clear();
txtRequired.Clear();
}
private void btnSave_Click(object sender, EventArgs e)
{
Save();
}
public void Save()
{
//create connection string
string connstring = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=fairdealdbAnalysis;Data Source=RECEPTION\\SQLEXPRESS";
//create connection to teh sql server database
SqlConnection sqlconn =new SqlConnection(connstring);
try
{
//open the connection
if(sqlconn.State!=ConnectionState.Open)
{
sqlconn.Open();
}
SqlCommand sqlcom = sqlconn.CreateCommand();
StringBuilder builder = new StringBuilder();
builder.Append("'"+txtPNo.Text+"',");
builder.Append("'"+txtPName.Text+"',");
builder.Append("'"+txtSiteName.Text+"',");
builder.Append("'"+txtJCNo.Text+"',");
builder.Append("'"+txtRequired.Text+"'");
sqlcom.CommandText ="INSERT INTO tblFormSave(dateIssued,projectNumber,projectName,jobCardNumber,siteName,RequiredBy) VALUES("+builder.ToString()+") ";
if(Convert.ToBoolean(sqlcom.ExecuteNonQuery()))
{
MessageBox.Show("Record Saved");
}
else
{
MessageBox.Show("Record not Saved");
}
}
catch{}
finally
{
if(sqlconn.State!=ConnectionState.Closed)
sqlconn.Close();
}
}
}
}
i was creating a code that wen one says save the data entered is saved into the sql server database.Kindly have a look at this.
i am getting errors...especially with the sql connection.