2012-02-29 11 views
5

हैलो मैं अपने एसक्यूएल में सी # WinForm के निर्माण के माध्यम से कुछ मूल्य लिखने की कोशिश कर रहा हूं। मुझे एक त्रुटि मिल रही है जिसे मैं हल नहीं कर सकता।एसक्यूएल अपवाद, अनुपलब्ध पैरामीटरयुक्त क्वेरी

मैं इस बोल रहा हूँ:

SQL.insertIntoChildTable(Convert.ToInt32(child_IDTextBox.Text), 
         child_FirstNameTextBox.Text, 
         child_LastNameTextBox.Text, 
         Convert.ToInt32(parent1IDTextBox.Text), 
         Convert.ToInt32(parent2IDTextBox.Text), 
         birthdayDateTimePicker.Value.ToShortDateString(), 
         age.ToString(), 
         null, null, null, null, null, 
         child_disciplineTextBox.Text, child_NotesTextBox.Text); 

इस से:

public static void insertIntoChildTable(int childID, string firstName, string lastName, int parent1ID, int parent2ID, string birthdate, string age, string lastCheckedInTime, string lastCheckedInBy, string lastCheckOutTime, string lastCheckedOutBy, string ageGroup, string disciplineNotes, string otherNotes) 
{ 
    try 
    { 
     using (SqlConnection connection = new SqlConnection(Global.connectionString)) 
     using (SqlCommand command = connection.CreateCommand()) 
     { 
      command.CommandText = "INSERT INTO ProjectList (ChildID, FirstName, LastName, Parent1ID, Parent2ID, Birthdate, Age, LastCheckedInTime, LastCheckedInBy, LastCheckOutTime, LastCheckedOutBy, AgeGroup, DisciplineNotes, OtherNotes) VALUES (@childID, @firstName, @lastName, @parent1ID, @parent2ID, @birthdate, @age, @lastCheckedInTime, @lastCheckedInBy, @lastCheckOutTime, @lastCheckedOutBy, @ageGroup, @disciplineNotes, @otherNotes)"; 

      command.Parameters.AddWithValue("@childID", childID); 
      command.Parameters.AddWithValue("@firstName", firstName); 
      command.Parameters.AddWithValue("@lastName", lastName); 
      command.Parameters.AddWithValue("@parent1ID", parent1ID); 
      command.Parameters.AddWithValue("@parent2ID", parent2ID); 
      command.Parameters.AddWithValue("@birthdate", birthdate); 
      command.Parameters.AddWithValue("@age", age); 
      command.Parameters.AddWithValue("@lastCheckedInTime", lastCheckedInTime); 
      command.Parameters.AddWithValue("@lastCheckedInBy", lastCheckedInBy); 
      command.Parameters.AddWithValue("@lastCheckOutTime", lastCheckOutTime); 
      command.Parameters.AddWithValue("@lastCheckedOutBy", lastCheckedOutBy); 
      command.Parameters.AddWithValue("@ageGroup", ageGroup); 
      command.Parameters.AddWithValue("@disciplineNotes", disciplineNotes); 
      command.Parameters.AddWithValue("@otherNotes", otherNotes); 

      connection.Open(); 
      command.ExecuteNonQuery(); 
     } 
    } 
    catch (SqlException ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
} 

और यह हो रही है:

प्रकार का एक पहला मौका अपवाद 'System.Data.SqlClient.SqlException 'System.Data.dll
पैरामीटरयुक्त क्वेरी' (@childID int, @ firstName nvarchar (4) में हुई, @lastName nvarchar (5), @ pare 'पैरामीटर ' @lastCheckedInTime 'की अपेक्षा करता है, जिसे आपूर्ति नहीं की गई थी।

कोई विचार?

+1

सोचें कि आप कमांड निर्दिष्ट करना चाहते हैं। कमांड टाइप = कमांड टाइप। टेक्स्ट स्पष्ट होने के लिए (समस्या का कारण नहीं बल्कि अच्छा अभ्यास)। – kaj

उत्तर

11

मुझे लगता है कि यह null है। पैरामीटर जो null जोड़े नहीं गए हैं। इसे DBNull.Value होना चाहिए। आप प्रत्येक पैरामीटर में ?? DBNull.Value जोड़ कर ऐसा कर सकते हैं। बेवकूफ, मुझे पता है:

command.Parameters.AddWithValue("@lastCheckInTime", 
     lastCheckInTime ?? DBNull.Value); 
हर पैरामीटर के लिए

; या उन पर पाश बाद में, किसी भी फिक्सिंग nullDBNull.Value रहे हैं:

foreach(var param in command.Parameters) { 
    if(param.Value == null) param.Value = DBNull.Value; 
} 

एक तरफ बात के रूप में - सभी string के रूप में उन होने बहुत संभावना नहीं लगता है, DateTime या DateTime? होना चाहिए, नहीं?

+0

yup 'null' SQL में अलग-अलग काम करता है। धन्यवाद। और मैं सब कुछ पाठ में परिवर्तित कर रहा हूं क्योंकि मैं बस इस चीज़ को पाने और चलाने की कोशिश कर रहा हूं। मूल्यों को सही प्रकार पर कास्ट करना होगा, मुझे बस और अनुभव चाहिए। एक बार फिर धन्यवाद। – ikathegreat

संबंधित मुद्दे