473,503 Members | 6,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB in ASP.NET- Writing Variables to an MSSQL Database

3 New Member
I am extremely new to programming and Visual Basic.

I want to know how to write/insert variables into an MS SQL database. When I say variables I mean input from textboxes and the current date/time generated.

I am currently using this code which I got from a tutorial and it basically writes the defined constants to the database.

Expand|Select|Wrap|Line Numbers
  1. Sub AddRecord() Dim sConnectionString As String _
  2.  
  3.  
  4. = "User ID=********;Password='*****';Initial Catalog=******;Data Source=p3swhsql-v16.shr.phx3.secureserver.net"
  5. Dim objConn As New SqlConnection(sConnectionString)
  6. objConn.Open()
  7. Dim sSQL As String = "INSERT INTO Employee " & _
  8. "(emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date)" & _
  9. "VALUES ('EBS123', 'Ebrahim', 'M', 'E', " & _
  10. "10, 82,'0877','2001-01-01')"
  11.  
  12. Dim objCmd As New SqlCommand(sSQL, objConn)
  13. Try
  14. objCmd.ExecuteNonQuery()
  15. Catch e As Exception
  16. Console.WriteLine(e.Message)
  17. Dim t = (e.Message)
  18. Literal1.Text = t
  19. Literal3.Text = "ERROR"
  20. End Try
  21. Console.WriteLine("Record Added")Literal2.Text = "RECORD ADDED"
  22.  
  23. End Sub

(Literal1 and Literal2 are two literals I have on the page to display the Error Received/IF record added)


But when I try to get the same script to insert variables from textboxes, I get an error saying

The name "empvar" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim empvar,fnameVar,lnameVar
  3. empvar=TextBox1.Text
  4. . ..
  5. Dim sSQL As String = "INSERT INTO Employee " & _
  6. "(emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date)" & _
  7. "VALUES (empvar,fnameVar,lnameVar, " & _
  8. "10, 82,'0877','2001-01-01')"
  9.  
Could you please assist me by telling me what I have done wrong in my script; or if the entire script does not allow for writing variables then could you tell me a simple one which can do that.

Thanking you very much in advanced
Jul 27 '08 #1
7 1463
DrBunchman
979 Recognized Expert Contributor
Hi bazeem,

You need to put your VB variables outside the SQL string like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. "VALUES ('" & empvar & "', '" & fnameVar & "', '" & lnameVar "' "
  3.  
In this way the value of the variables empvar, fnameVar & lnameVar will be passed into the SQL String. Note the single quotes that surround each variable - these are required if the database column is a string.

Does this make sense?

Dr B
Jul 28 '08 #2
kenobewan
4,871 Recognized Expert Specialist
Suggestion, strongly advise against inserting id this way can lead to errors and probs later - not scalable.
Jul 28 '08 #3
Curtis Rutland
3,256 Recognized Expert Specialist
I agree with Kenobewan. Take a look at this thread. The 6th post. I give the OP an example of how to use a parameterized query. This is a better way to do it.
Jul 28 '08 #4
DrBunchman
979 Recognized Expert Contributor
I agree also.

I actually thought Kenobewan's reply was referring to the OP inserting an ID column rather than the non-use of parameters..?

Dr B
Jul 28 '08 #5
Curtis Rutland
3,256 Recognized Expert Specialist
I think you are right. I read it too quickly. Still, parameters are easier and more elegant than building strings.
Jul 28 '08 #6
DrBunchman
979 Recognized Expert Contributor
They certainly are. I was aiming to keep my reply in context with the tutorial the OP had been looking at but I guess it makes sense to get into good habits early on!

Dr B
Jul 28 '08 #7
kenobewan
4,871 Recognized Expert Specialist
Agree that parameters are better practice. If the table is employee and OP refers to emp_id I would want to that up as the primary key not for replication, default 1 increment 1. I never let the application set the id of the table, if this is indeed the situation :o).
Jul 30 '08 #8

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

Similar topics

3
4032
by: lawrence | last post by:
I haven't been able to reach www.php.net for days. Most of the rest of the web is working for me, though I've bad trouble reaching any English sites. Anyone else having trouble?
9
3099
by: gulu man | last post by:
Hi, What is the substitute for COM objects in .NET? How can I create something similar to com in .net? Is it still possible? Thank you
3
3041
by: hussein | last post by:
'm not bale to run my projec on the 2000 server, he error was: error while trying to run project: unable to start debugging on the web server.erver side error occurred on sending debug HTTP request...
9
2744
by: Bob | last post by:
Is ASP.NET 1.1 available on the 64 bit extended version of Windows 2003 Server? When I install VS.NET 2003 I then get Service Unavailable from IIS when navigating to the main under construction...
132
4712
by: Kevin Spencer | last post by:
About 2 years ago, and as recently as perhaps 1 year ago, I can recall seeing many posts about what language to use with ASP.Net. The consensus was that employers paid more for C# programmers, and...
13
1592
by: Kenneth Windish | last post by:
Hi, I wrote a simple web application using web matrix. When I run it on my local computer all works fine, but when I run it on hosting site all hyperlinks are not working and just postback to...
13
1527
by: Steve H. | last post by:
Hi all, I'm searching for a few good books on asp.net and c#. I'm afraid of buying books here in denmark that i cant read though before i get them, becuase 90% of them are just pure junk. I...
1
3353
by: abh1508 | last post by:
Following a release of code the following problem occurs on certain asp ..net pages. This is not a problem on other testing/demo environments. IIS seems to be creating certain files twice in the...
14
1716
by: David Lozzi | last post by:
Hello, I am in need of some advice. I want to move to .Net 2.0 and Visual Studio 2005 for ASP.NET developement, however I'm hesitant. Here are a few questions I have regarding the upgrade: 1)...
22
3236
by: EP | last post by:
When running my asp.net hosting service (asp.net without IIS), on server 2003 with IIS not installed, I get the following when trying to process a request. "System.DllNotFoundException: Unable to...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.