I'm trying to update a SQL Server 2005 database with data entered by the user into textboxes. Here is the code I am using:
And here is the exception I keep getting:
System.Exceptio n was unhandled
Message="Error Inserting"
Source="MLMLoca lAdmin"
StackTrace:
at MLMLocalAdmin.f rmAddDistributo r.btnSave2_Clic k(Object sender, EventArgs e) in C:\Users\Chis\D ocuments\Visual Studio 2005\Projects\M LMLocalAdmin\ML MLocalAdmin\frm AddDistributor. cs:line 120
at System.Windows. Forms.Control.O nClick(EventArg s e)
at System.Windows. Forms.Button.On Click(EventArgs e)
at System.Windows. Forms.Button.On MouseUp(MouseEv entArgs mevent)
at System.Windows. Forms.Control.W mMouseUp(Messag e& m, MouseButtons button, Int32 clicks)
at System.Windows. Forms.Control.W ndProc(Message& m)
at System.Windows. Forms.ButtonBas e.WndProc(Messa ge& m)
at System.Windows. Forms.Button.Wn dProc(Message& m)
at System.Windows. Forms.Control.C ontrolNativeWin dow.OnMessage(M essage& m)
at System.Windows. Forms.Control.C ontrolNativeWin dow.WndProc(Mes sage& m)
at System.Windows. Forms.NativeWin dow.DebuggableC allback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows. Forms.UnsafeNat iveMethods.Disp atchMessageW(MS G& msg)
at System.Windows. Forms.Applicati on.ComponentMan ager.System.Win dows.Forms.Unsa feNativeMethods .IMsoComponentM anager.FPushMes sageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo pInner(Int32 reason, ApplicationCont ext context)
at System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo p(Int32 reason, ApplicationCont ext context)
at System.Windows. Forms.Applicati on.Run(Form mainForm)
at MLMLocalAdmin.P rogram.Main() in C:\Users\Chis\D ocuments\Visual Studio 2005\Projects\M LMLocalAdmin\ML MLocalAdmin\Pro gram.cs:line 17
at System.AppDomai n._nExecuteAsse mbly(Assembly assembly, String[] args)
at System.AppDomai n.ExecuteAssemb ly(String assemblyFile, Evidence assemblySecurit y, String[] args)
at Microsoft.Visua lStudio.Hosting Process.HostPro c.RunUsersAssem bly()
at System.Threadin g.ThreadHelper. ThreadStart_Con text(Object state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext executionContex t, ContextCallback callback, Object state)
at System.Threadin g.ThreadHelper. ThreadStart()
Can somebody please help me!!!
Code:
namespace MLMLocalAdmin
{
public partial class frmAddDistributor : Form
{
public frmAddDistributor()
{
InitializeComponent();
}
private void btnSave2_Click(object sender, EventArgs e)
{
if (txtTianshiCode.Text == "")
{
MessageBox.Show("Please enter the distributor's Tianshi code");
}
else if (txtFullname.Text == "")
{
MessageBox.Show("Please enter the distributor's full name");
}
else if (txtGender2.Text == "")
{
MessageBox.Show("Please enter the gender");
}
else if (txtNRC.Text == "")
{
MessageBox.Show("Please enter the NRC or ID number");
}
else
{
//stores the values entered as variables
string tianshiCode = txtTianshiCode.Text;
string fullname = txtFullname.Text;
string gender = txtGender2.Text;
string nrc = txtNRC.Text;
string nationality = txtNationality.Text;
string birthDate = txtBirthDate.Text;
string address = txtAddress.Text;
string postalCode = txtPostalCode.Text;
string city = txtCity.Text;
string stateProv = txtStateProv.Text;
string homeTel = txtHomeTel.Text;
string officeTel = txtOfficeTel.Text;
string mobileTel = txtMobileTel.Text;
string email = txtEmail.Text;
string joinDate = txtJoinDate.Text;
string sponsorTianshiCode = txtSponsorTianshiCode.Text;
//establishes a connection with the database
SqlConnection cn;
SqlCommand cmd;
//SqlDataReader datareader;
string sql;
try
{
// this query is for insertion into the distributor table
sql = "INSERT INTO Distributor (tianshiCode, fullName, gender, NRC_ID, nationality, birthDate, address, postalCode, city, stateProvince, homeTel, officeTel, mobileTel, email, sponsorTianshiCode, joinDate)";
sql += String.Format("VALUES, @tianshiCode, @fullname, @gender, @nrc, @nationality, @birthDate, @address, @postalCode, @city, @stateProv, @homeTel, @officeTel, @mobileTel, @email, @sponsorTianshiCode, @joinDate");
cn = new SqlConnection(Properties.Settings.Default.MLM_DB1ConStr);
cmd = new SqlCommand(sql, cn);
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@tianshiCode", txtTianshiCode.Text);
cmd.Parameters.AddWithValue("@fullName", txtFullname.Text);
cmd.Parameters.AddWithValue("@gender", txtGender2.Text);
cmd.Parameters.AddWithValue("@nrc", txtNRC.Text);
cmd.Parameters.AddWithValue("@nationality", txtNationality.Text);
cmd.Parameters.AddWithValue("@birthDate", txtBirthDate.Text);
cmd.Parameters.AddWithValue("@address", txtAddress.Text);
cmd.Parameters.AddWithValue("@postalCode", txtPostalCode.Text);
cmd.Parameters.AddWithValue("@city", txtCity.Text);
cmd.Parameters.AddWithValue("@stateProv", txtStateProv.Text);
cmd.Parameters.AddWithValue("@homeTel", txtHomeTel.Text);
cmd.Parameters.AddWithValue("@officeTel", txtOfficeTel.Text);
cmd.Parameters.AddWithValue("@mobileTel", txtMobileTel.Text);
cmd.Parameters.AddWithValue("@email", txtEmail.Text);
cmd.Parameters.AddWithValue("@sponsorTianshiCode", txtSponsorTianshiCode.Text);
cmd.Parameters.AddWithValue("@joinDate", txtJoinDate.Text);
cn.Open(); //opens connection
cmd.ExecuteNonQuery(); //writes to the database
MessageBox.Show("Update Successful!!");
}
catch (SqlException ex)
{
throw new Exception("Error Inserting", ex);
}
cn.Close();
} //closes the else block
}
private void addNewToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void frmAddDistributor_Load(object sender, EventArgs e)
{
}
}
}
System.Exceptio n was unhandled
Message="Error Inserting"
Source="MLMLoca lAdmin"
StackTrace:
at MLMLocalAdmin.f rmAddDistributo r.btnSave2_Clic k(Object sender, EventArgs e) in C:\Users\Chis\D ocuments\Visual Studio 2005\Projects\M LMLocalAdmin\ML MLocalAdmin\frm AddDistributor. cs:line 120
at System.Windows. Forms.Control.O nClick(EventArg s e)
at System.Windows. Forms.Button.On Click(EventArgs e)
at System.Windows. Forms.Button.On MouseUp(MouseEv entArgs mevent)
at System.Windows. Forms.Control.W mMouseUp(Messag e& m, MouseButtons button, Int32 clicks)
at System.Windows. Forms.Control.W ndProc(Message& m)
at System.Windows. Forms.ButtonBas e.WndProc(Messa ge& m)
at System.Windows. Forms.Button.Wn dProc(Message& m)
at System.Windows. Forms.Control.C ontrolNativeWin dow.OnMessage(M essage& m)
at System.Windows. Forms.Control.C ontrolNativeWin dow.WndProc(Mes sage& m)
at System.Windows. Forms.NativeWin dow.DebuggableC allback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows. Forms.UnsafeNat iveMethods.Disp atchMessageW(MS G& msg)
at System.Windows. Forms.Applicati on.ComponentMan ager.System.Win dows.Forms.Unsa feNativeMethods .IMsoComponentM anager.FPushMes sageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo pInner(Int32 reason, ApplicationCont ext context)
at System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo p(Int32 reason, ApplicationCont ext context)
at System.Windows. Forms.Applicati on.Run(Form mainForm)
at MLMLocalAdmin.P rogram.Main() in C:\Users\Chis\D ocuments\Visual Studio 2005\Projects\M LMLocalAdmin\ML MLocalAdmin\Pro gram.cs:line 17
at System.AppDomai n._nExecuteAsse mbly(Assembly assembly, String[] args)
at System.AppDomai n.ExecuteAssemb ly(String assemblyFile, Evidence assemblySecurit y, String[] args)
at Microsoft.Visua lStudio.Hosting Process.HostPro c.RunUsersAssem bly()
at System.Threadin g.ThreadHelper. ThreadStart_Con text(Object state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext executionContex t, ContextCallback callback, Object state)
at System.Threadin g.ThreadHelper. ThreadStart()
Can somebody please help me!!!