473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Visual C# code for entering data from textboxes on form to SQL Server 2005 database

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:


Expand|Select|Wrap|Line Numbers
  1. namespace MLMLocalAdmin
  2. {
  3.     public partial class frmAddDistributor : Form
  4.     {
  5.  
  6.         public frmAddDistributor()
  7.         {
  8.             InitializeComponent();
  9.         }
  10.  
  11.  
  12.         private void btnSave2_Click(object sender, EventArgs e)
  13.         {
  14.             if (txtTianshiCode.Text == "")
  15.             {
  16.                 MessageBox.Show("Please enter the distributor's Tianshi code");
  17.             }
  18.             else if (txtFullname.Text == "")
  19.             {
  20.                 MessageBox.Show("Please enter the distributor's full name");
  21.             }
  22.             else if (txtGender2.Text == "")
  23.             {
  24.                 MessageBox.Show("Please enter the gender");
  25.             }
  26.             else if (txtNRC.Text == "")
  27.             {
  28.                 MessageBox.Show("Please enter the NRC or ID number");
  29.             }
  30.  
  31.             else
  32.             {
  33.                 //stores the values entered as variables
  34.                 string tianshiCode = txtTianshiCode.Text;
  35.                 string fullname = txtFullname.Text;
  36.                 string gender = txtGender2.Text;
  37.                 string nrc = txtNRC.Text;
  38.                 string nationality = txtNationality.Text;
  39.                 string birthDate = txtBirthDate.Text;
  40.                 string address = txtAddress.Text;
  41.                 string postalCode = txtPostalCode.Text;
  42.                 string city = txtCity.Text;
  43.                 string stateProv = txtStateProv.Text;
  44.                 string homeTel = txtHomeTel.Text;
  45.                 string officeTel = txtOfficeTel.Text;
  46.                 string mobileTel = txtMobileTel.Text;
  47.                 string email = txtEmail.Text;
  48.                 string joinDate = txtJoinDate.Text;
  49.                 string sponsorTianshiCode = txtSponsorTianshiCode.Text;
  50.  
  51.                 //establishes a connection with the database
  52.                 SqlConnection cn;
  53.                 SqlCommand cmd;
  54.                 //SqlDataReader datareader;
  55.                 string sql;
  56.  
  57.                 try
  58.                 {
  59.  
  60.                     // this query is for insertion into the distributor table
  61.                     sql = "INSERT INTO Distributor (tianshiCode, fullName, gender, NRC_ID, nationality, birthDate, address, postalCode, city, stateProvince, homeTel, officeTel, mobileTel, email, sponsorTianshiCode, joinDate)";
  62.                     sql += String.Format("VALUES, @tianshiCode, @fullname, @gender, @nrc, @nationality, @birthDate, @address, @postalCode, @city, @stateProv, @homeTel, @officeTel, @mobileTel, @email, @sponsorTianshiCode, @joinDate");
  63.  
  64.                     cn = new SqlConnection(Properties.Settings.Default.MLM_DB1ConStr);
  65.                     cmd = new SqlCommand(sql, cn);
  66.                     cmd.CommandText = sql;
  67.                     cmd.CommandType = CommandType.Text;
  68.  
  69.  
  70.                     cmd.Parameters.AddWithValue("@tianshiCode", txtTianshiCode.Text);
  71.                     cmd.Parameters.AddWithValue("@fullName", txtFullname.Text);
  72.                     cmd.Parameters.AddWithValue("@gender", txtGender2.Text);
  73.                     cmd.Parameters.AddWithValue("@nrc", txtNRC.Text);
  74.                     cmd.Parameters.AddWithValue("@nationality", txtNationality.Text);
  75.                     cmd.Parameters.AddWithValue("@birthDate", txtBirthDate.Text);
  76.                     cmd.Parameters.AddWithValue("@address", txtAddress.Text);
  77.                     cmd.Parameters.AddWithValue("@postalCode", txtPostalCode.Text);
  78.                     cmd.Parameters.AddWithValue("@city", txtCity.Text);
  79.                     cmd.Parameters.AddWithValue("@stateProv", txtStateProv.Text);
  80.                     cmd.Parameters.AddWithValue("@homeTel", txtHomeTel.Text);
  81.                     cmd.Parameters.AddWithValue("@officeTel", txtOfficeTel.Text);
  82.                     cmd.Parameters.AddWithValue("@mobileTel", txtMobileTel.Text);
  83.                     cmd.Parameters.AddWithValue("@email", txtEmail.Text);
  84.                     cmd.Parameters.AddWithValue("@sponsorTianshiCode", txtSponsorTianshiCode.Text);
  85.                     cmd.Parameters.AddWithValue("@joinDate", txtJoinDate.Text);
  86.  
  87.  
  88.                     cn.Open(); //opens connection
  89.                     cmd.ExecuteNonQuery(); //writes to the database
  90.                     MessageBox.Show("Update Successful!!");
  91.                 }
  92.  
  93.  
  94.                 catch (SqlException ex)
  95.                 {
  96.                     throw new Exception("Error Inserting", ex);
  97.  
  98.                 }
  99.  
  100.                 cn.Close();
  101.             } //closes the else block
  102.  
  103.  
  104.         }
  105.  
  106.         private void addNewToolStripMenuItem1_Click(object sender, EventArgs e)
  107.         {
  108.  
  109.         }
  110.  
  111.         private void frmAddDistributor_Load(object sender, EventArgs e)
  112.         {
  113.  
  114.         }
  115.     }
  116. }
  117.  
  118.  
And here is the exception I keep getting:



System.Exception was unhandled
Message="Error Inserting"
Source="MLMLocalAdmin"
StackTrace:
at MLMLocalAdmin.frmAddDistributor.btnSave2_Click(Obj ect sender, EventArgs e) in C:\Users\Chis\Documents\Visual Studio 2005\Projects\MLMLocalAdmin\MLMLocalAdmin\frmAddDi stributor.cs:line 120
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventAr gs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at MLMLocalAdmin.Program.Main() in C:\Users\Chis\Documents\Visual Studio 2005\Projects\MLMLocalAdmin\MLMLocalAdmin\Program. cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()



Can somebody please help me!!!
May 3 '10 #1
0 1377

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: CDMAPoster | last post by:
In: http://groups.google.com/group/comp.databases.ms-access/msg/ec3b99fed4b094ed I said: Here's some more information on managed code UDF's in SQL Server 2005:
4
by: itsnidhu | last post by:
Hi this is nidhi from india, Since last 2 days i am facing a problem in asp.net code. The data base connection is done correctly as i can select the data from the database. But i can not insert...
1
by: DavidCh | last post by:
Is there any way to do that? (Restoring a SQL Server 2005 database in SQL Server 2000) I know that there's not an automatic way, but i wanna know if there's some type of backup or something like...
0
by: tapasi | last post by:
Hi, Can Anyone please tell me how to import 2000 database in vb.net 2003 by code. As well backup of sql server 2005 database in vb.net by code? Thanks in Advance.
0
Dököll
by: Dököll | last post by:
ASP.NET and SQL Server 2005 Database interaction includes: Language: ASP.NET Connectivity: SQL Server 2005 Foreword: This database connectivity is nothing of genius, it is a simple...
2
by: moorcroft | last post by:
Hi I am coding an ASP .Net project in C# and am using connection strings to connect to a Microsoft SQL Server 2005 database, and the project involves tracking student attendance records at a...
2
by: =?Utf-8?B?U2hlcndvb2Q=?= | last post by:
Greetings, I am using Visual C#.NET 2005 (Express Edition) and want to connect to a SQL Server 2005 (Express Edition) database. When I attempt to compile the code below, however, I receive the...
1
JustRun
by: JustRun | last post by:
I have created a SQL Server 2005 database and I plan to protect it with a username and password. I'm unclear as to how to do this. How can I make a security system for my new database with its...
1
by: Gurpreet Singhh | last post by:
whats error in java statement to insert data into sql server 2005??? # String query = "insert into dbo.guru VALUES('"taskdata.getTaskid() +',' + taskdata.getTaskname()...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.