<% ' Initialize error handling Call InitializeErrorHandling("savevendor.asp") ' Get and validate all inputs Dim vendor, isprinter, ispc, ismachine vendor = Trim(Request.Form("vendor")) isprinter = Request.Form("isprinter") ispc = Request.Form("ispc") ismachine = Request.Form("ismachine") ' Validate required fields If vendor = "" Then Call HandleValidationError("addvendor.asp", "INVALID_INPUT") End If ' Validate field lengths If Len(vendor) > 50 Then Call HandleValidationError("addvendor.asp", "INVALID_INPUT") End If ' Check if at least one type is selected If isprinter <> "1" AND ispc <> "1" AND ismachine <> "1" Then Response.Write("
Error: Please select at least one category for this manufacturer.
") Response.Write("Go back") Call CleanupResources() Response.End End If ' Check if vendor already exists Dim checkSQL, rsCheck checkSQL = "SELECT COUNT(*) as cnt FROM vendors WHERE LOWER(vendor) = LOWER(?)" Set rsCheck = ExecuteParameterizedQuery(objConn, checkSQL, Array(vendor)) If Not rsCheck.EOF Then If Not IsNull(rsCheck("cnt")) Then If CLng(rsCheck("cnt")) > 0 Then rsCheck.Close Set rsCheck = Nothing Response.Write("
Error: Manufacturer '" & Server.HTMLEncode(vendor) & "' already exists.
") Response.Write("Go back") Call CleanupResources() Response.End End If End If End If rsCheck.Close Set rsCheck = Nothing ' Insert the new vendor Dim vendorSQL, vendorParams Dim isPrinterVal, isPcVal, isMachineVal If isprinter = "1" Then isPrinterVal = 1 Else isPrinterVal = 0 If ispc = "1" Then isPcVal = 1 Else isPcVal = 0 If ismachine = "1" Then isMachineVal = 1 Else isMachineVal = 0 vendorSQL = "INSERT INTO vendors (vendor, isactive, isprinter, ispc, ismachine) VALUES (?, 1, ?, ?, ?)" vendorParams = Array(vendor, CInt(isPrinterVal), CInt(isPcVal), CInt(isMachineVal)) Dim recordsAffected recordsAffected = ExecuteParameterizedInsert(objConn, vendorSQL, vendorParams) ' Get the new vendor ID Dim newVendorId Set rsCheck = objConn.Execute("SELECT LAST_INSERT_ID() as newid") newVendorId = 0 If Not rsCheck.EOF Then If Not IsNull(rsCheck("newid")) Then newVendorId = CLng(rsCheck("newid")) End If End If rsCheck.Close Set rsCheck = Nothing ' Cleanup resources Call CleanupResources() ' Redirect back to where they came from or to a success page If recordsAffected > 0 And newVendorId > 0 Then Response.Write("
Manufacturer added successfully!
") Response.Write("

Manufacturer '" & Server.HTMLEncode(vendor) & "' has been added.

") Response.Write("

Add Another Manufacturer ") Response.Write("Add Model

") Else Response.Write("
Error: Manufacturer was not added successfully.
") Response.Write("Go back") End If %>