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. Sub AddRecord() Dim sConnectionString As String _
-
-
-
= "User ID=********;Password='*****';Initial Catalog=******;Data Source=p3swhsql-v16.shr.phx3.secureserver.net"
-
Dim objConn As New SqlConnection(sConnectionString)
-
objConn.Open()
-
Dim sSQL As String = "INSERT INTO Employee " & _
-
"(emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date)" & _
-
"VALUES ('EBS123', 'Ebrahim', 'M', 'E', " & _
-
"10, 82,'0877','2001-01-01')"
-
-
Dim objCmd As New SqlCommand(sSQL, objConn)
-
Try
-
objCmd.ExecuteNonQuery()
-
Catch e As Exception
-
Console.WriteLine(e.Message)
-
Dim t = (e.Message)
-
Literal1.Text = t
-
Literal3.Text = "ERROR"
-
End Try
-
Console.WriteLine("Record Added")Literal2.Text = "RECORD ADDED"
-
-
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 -
-
Dim empvar,fnameVar,lnameVar
-
empvar=TextBox1.Text
-
. ..
-
Dim sSQL As String = "INSERT INTO Employee " & _
-
"(emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date)" & _
-
"VALUES (empvar,fnameVar,lnameVar, " & _
-
"10, 82,'0877','2001-01-01')"
-
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
7 1463
Hi bazeem,
You need to put your VB variables outside the SQL string like this: -
-
"VALUES ('" & empvar & "', '" & fnameVar & "', '" & lnameVar "' "
-
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
Suggestion, strongly advise against inserting id this way can lead to errors and probs later - not scalable.
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.
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
I think you are right. I read it too quickly. Still, parameters are easier and more elegant than building strings.
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
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).
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |