2017-04-05 3 views
0

나는 페이지를 실행하려고하면이 오류 메시지가 계속 ..매개 변수화 된 쿼리

매개 변수화 된 쿼리 '(@Medication의 NVARCHAR (14), @ 수량 NVARCHAR (9), @ RequestedDate N' 공급되지 않은 매개 변수 '@RequestedDate'를 기대

내 코드 숨김 파일입니다.

Dim conn As SqlConnection 
Dim cmd As SqlCommand 

'Getting today's date to store into database to let staff know when prescription was ordered 
'reference for this code : - https://msdn.microsoft.com/en-us/library/system.datetime.today(v=vs.110).aspx 

Dim thisDay As DateTime = DateTime.Today 
Session("DateRequested") = (thisDay.ToString()) 
Dim Medication As String 
Dim Quantity As String 
Dim RequestedDate As String 
Dim Pharmacy As String 
Dim PatientNumber As String 

Medication = txtDrug1.Text + " " + txtDrug2.Text + " " + txtDrug3.Text + " " + txtDrug4.Text + " " + txtDrug5.Text 
Quantity = txtQuant1.Text + " " + txtQuant2.Text + " " + txtQuant3.Text + " " + txtQuant4.Text + " " + txtQuant5.Text 

RequestedDate = Session("Date Requested") 
PatientNumber = Session("PatientNumber") 
Pharmacy = txtPharmacy.Text 

Dim cmdstring As String = "INSERT INTO Prescription (Medication, QntyandStrength, RequestedDate, Pharmacy, PatientNumber) Values (@Medication, @Quantity, @RequestedDate, @Pharmacy, @PatientNumber)" 

conn = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\Documents\Visual Studio 2015\WebSites\myAppointments\App_Data\Database.mdf;Integrated Security=True") 
cmd = New SqlCommand(cmdstring, conn) 

cmd.Parameters.AddWithValue("@Medication", Medication) 
cmd.Parameters.AddWithValue("@Quantity", Quantity) 
cmd.Parameters.AddWithValue("@RequestedDate", RequestedDate) 
cmd.Parameters.AddWithValue("@Pharmacy", Pharmacy) 
cmd.Parameters.AddWithValue("@PatientNumber", PatientNumber) 

conn.Open() 

cmd.ExecuteNonQuery() 
conn.Close() 

MsgBox("Your Prescription Has Been Requested!") 

그리고 데이터베이스 유형은 다음과 같습니다

[PrescriptionNumber] INT   NOT NULL, 
[Medication]   NVARCHAR(250) NULL, 
[QntyandStrength] NVARCHAR(250) NULL, 
[RequestedDate]  NVARCHAR(70) NULL, 
[Pharmacy]   VARCHAR (50) NULL, 
[PatientNumber]  CHAR (10) NULL, 

변경 방법에 대한 조언이 있으십니까?

+4

왜 날짜를 문자열로 저장하고 있습니까? –

+0

'.AddWithValue' 대신'.Add'를 사용하십시오. – Bugs

+0

'RequestedDate = Session ("Requested")''RequestedDate'가'Nothing' 대신에 _actual_ 값을 가지고 있다고 확신합니까? – Cameron

답변

0

Chris Dunaway가 지적한대로 세션 변수에 대한 참조가 잘못되었습니다. 당신이 매개 변수를 통과 할 때, 그래서 ...

RequestedDate = Session("Date Requested") ''This variable name has a space. 

Session("DateRequested") = (thisDay.ToString()) 

...하지만 당신은 다른 이름으로 참조 :

당신은 변수를 선언하고 값을 할당 ...

cmd.Parameters.AddWithValue("@RequestedDate", RequestedDate) 

... null 개체에 대한 참조를 전달 중입니다. 세션이 없습니다 ("요청 된 날짜").

+0

안녕하세요 모두들, 저의 바보 같은 눈을 가진 실수는 세션에서 공간이었습니다, 마침내 작동합니다, 도움을 주셔서 감사합니다! –