473,382 Members | 1,204 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,382 software developers and data experts.

Error while trying to access the sql server

I am trying to insert values into the table using the values from the form

I following is the code I am using
Expand|Select|Wrap|Line Numbers
  1. protected void Button_Click(object sender, EventArgs e)
  2. {
  3.     SqlConnection conn = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=MyFavs;Integrated Security=SSPI");
  4.  
  5.     string insertstring = @"Insert into MyFavs values (@FirstName,@LastName,@Address,@City,@State,@Phone,@Email,@Password)";
  6.     SqlCommand cmd = new SqlCommand(insertstring, conn);       
  7.  
  8.     cmd.Parameters.AddWithValue("@FirstName", SqlDbType.VarChar).Value = FirstNameTextBox.Text.ToString();
  9.     cmd.Parameters.AddWithValue("@LastName", SqlDbType.VarChar).Value = LastNameTextBox.Text.ToString();
  10.     cmd.Parameters.AddWithValue("@Address", SqlDbType.VarChar).Value = AddressTextBox.Text.ToString();
  11.     cmd.Parameters.AddWithValue("@City", SqlDbType.VarChar).Value = CityTextBox.Text.ToString();
  12.     cmd.Parameters.AddWithValue("@State", SqlDbType.VarChar).Value = StateTextBox.Text.ToString();
  13.     cmd.Parameters.AddWithValue("@Phone", SqlDbType.VarChar).Value = PhoneTextBox.Text.ToString();
  14.     cmd.Parameters.AddWithValue("@Email", SqlDbType.VarChar).Value = EmailTextBox.Text.ToString();
  15.     cmd.Parameters.AddWithValue("@Password", SqlDbType.VarChar).Value = PasswordTextBox.Text.ToString();
  16.  
  17.     try
  18.     {
  19.         conn.Open();
  20.         cmd.ExecuteNonQuery();
  21.     }
  22.     catch (SqlException er)
  23.     {
  24.         Response.Write(er.StackTrace);
  25.     }
  26.     finally
  27.     {
  28.         conn.Close();
  29.     }
I get the following error when I execute the code

at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteRead er(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderT ds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()



Any help would be appreciated.

Thanks,
Sahashra
Jun 11 '09 #1
1 1466
Frinavale
9,735 Expert Mod 8TB
I am unfamiliar with the AddWithValue method so I looked it up.
According to MSDN:
AddWithValue replaces the SqlParameterCollection.Add method that takes a String and an Object.
You are not passing it an Object, you are passing it Strings (text from the TextBox).

I recommend that you use the Add Method instead of the AddWithValue method since you are using strings.
Jun 16 '09 #2

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

Similar topics

5
by: Patrice FRITSCH | last post by:
I'm trying to run a batch file from an asp page using WScript.Shell object. Dim oWSH set oWSH= Server.CreateObject("WScript.Shell") call oWSH.Run("cmd.exe /c " & szCmd , 0, true) szCmd...
9
by: Marina Anufreichik | last post by:
Hi, After deploymnet web application on web server I can access page on local machine and login fine but when I'm trying to access web site from remote machine I can see login page, but when I'm...
1
by: Del | last post by:
We have a MS SQL Server 2000 application for our shipping system. We have created a front end application using MS Access for our order analyst. The front end application allows the analyst to...
7
by: astro | last post by:
Anyone have suggestions on where to troubleshoot this error? Background: -Access 2k v. 9.0.6926 sp3 - front and backend on production server (wiindows 2k) -accessed via Citrix -front-end is...
8
by: Razak | last post by:
Hi, I have a class which basically do Impersonation in my web application. From MS KB sample:- ++++++++++++++++++++code starts Dim impersonationContext As...
9
by: Tim D | last post by:
Hi, I originally posted this as a reply to a rather old thread in dotnet.framework.general and didn't get any response. I thought it might be more relevant here; anyone got any ideas? My...
14
by: Gianfranco | last post by:
Hi everybody, First of all, sorry for my english if it isn't perfect ;-p I have an error in an asp.net application. I have win2003 server, with iis 6. I'm developping with visual studio...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
23
by: deathtospam | last post by:
A day or two ago, I wrote a quick ASPX page with a CS codebehind using Visual Studio .NET 2005 -- it worked, I saved it and closed the project. Today, I came back to the project, reopened the...
10
by: gary0gilbert | last post by:
An unusual spin to this recurring disk or network error in a Terminal Server environment. Access 2000, Terminal Server 2000, file server is windows 2000. All users have a separate copy of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.