Ordering subsections of case statement - sql

So I have a query with the following sort order:
String sortOrder = "CASE " + ICCGoal.COLUMNS.category
+ " WHEN '" + GoalCategory.PHYSICAL_ACTIVITY.getEncodedValue() + "' THEN 1"
+ " WHEN '" + GoalCategory.SLEEP.getEncodedValue() + "' THEN 2"
+ " WHEN '" + GoalCategory.SOCIAL.getEncodedValue() + "' THEN 3"
+ " WHEN '" + GoalCategory.MOOD.getEncodedValue() + "' THEN 4"
+ " WHEN '" + GoalCategory.ENERGY.getEncodedValue() + "' THEN 5"
+ " END";
is there anyway to order the sections of the returned values?
Currently, I have all goals of "SLEEP" type returned after the "PHYSICAL_ACTIVITY" and before "SOCIAL" goals, in whatever order they are stored.
Would it be possible to return them ordered by date created? Or just ordered in another way than default?

Just add a secondary sorting criteria:
String sortOrder = "CASE " + ICCGoal.COLUMNS.category
+ " WHEN '" + GoalCategory.PHYSICAL_ACTIVITY.getEncodedValue() + "' THEN 1"
+ " WHEN '" + GoalCategory.SLEEP.getEncodedValue() + "' THEN 2"
+ " WHEN '" + GoalCategory.SOCIAL.getEncodedValue() + "' THEN 3"
+ " WHEN '" + GoalCategory.MOOD.getEncodedValue() + "' THEN 4"
+ " WHEN '" + GoalCategory.ENERGY.getEncodedValue() + "' THEN 5"
+ " END" // Original ordering from the OP
+ ", date_created"; // Secondary term

Related

How to convert from type Dbnull to type Date

If IsDBNull(dgv_datapasien.Rows(i).Cells(11).Value.ToString) Then
tglcetak = dgv_datapasien.Rows(i).Cells(11).Value.ToString
With dgv_datapasien.Rows(iRowIndex)
cmdData.CommandText = "Update tabelpasien set no_lab ='" + .Cells(1).Value.ToString + "', no_rm ='" + .Cells(2).Value.ToString + "'
, nama ='" + .Cells(3).Value.ToString + "',jeniskelamin ='" + .Cells(5).Value.ToString + "'
, status ='" + .Cells(6).Value.ToString + "', tglmasuk ='" & FormatTglUniversal(.Cells(8).Value) & "'
, ruangasal ='" + .Cells(7).Value.ToString + "' ,dokterpengirim='" + .Cells(9).Value.ToString + "'
,analis='" + .Cells(10).Value.ToString + "',umur ='" + .Cells(4).Value.ToString + "',analyzer='" + .Cells(0).Value.ToString + "'
where no_lab ='" + .Cells(1).Value.ToString + "'"
' ,tglcetak='" & FormatDateNull(.Cells(11).Value) & "'
End With
cmdData.ExecuteNonQuery()
End If
I need to update database from datagridview by clicking button Update.
One of the field is about datePrint.
But, sometimes, my dateprint is dbnull.
so I have problem to update database because datePrint is dbnull.
my code always show "Conversion from type 'DbNull' to 'Date' type is not valid"
fyi: I use postgresql
Anyone know how to solve this?
You should check for the datePrint cell during building the SQL query and set the respective column (in your case tglcetak) to NULL if the source value is null, otherwise set it to the date value.
Assuming your source grid cell(11) contains the datePrint value:
With dgv_datapasien.Rows(iRowIndex)
cmdData.CommandText = "Update tabelpasien set no_lab ='" + .Cells(1).Value.ToString +
"', no_rm ='" + .Cells(2).Value.ToString +
"', nama ='" + .Cells(3).Value.ToString +
"', jeniskelamin ='" + .Cells(5).Value.ToString +
"', status ='" + .Cells(6).Value.ToString +
"', tglmasuk ='" & FormatTglUniversal(.Cells(8).Value) &
"', ruangasal ='" + .Cells(7).Value.ToString +
"', dokterpengirim='" + .Cells(9).Value.ToString +
"', analis='" + .Cells(10).Value.ToString +
"', umur ='" + .Cells(4).Value.ToString +
"', analyzer='" + .Cells(0).Value.ToString + "'"
If IsDBNull(.Cells(11).Value) Then
cmdData.CommandText &= ",tglcetak=NULL"
Else
cmdData.CommandText &= ",tglcetak='" & FormatTglUniversal(.Cells(11).Value) & "'"
End If
cmdData.CommandText &= " where no_lab ='" + .Cells(1).Value.ToString + "'"
End With
cmdData.ExecuteNonQuery()
I don't know what your FormatDateNull function returns, so I don't use it above. And I assume your function FormatTglUniversal formats the date string to postgresql date format.
I would recommend you get used to using parameterized query for security and performance since your code here is inside a loop.
Make sure Date Field in database allows null
while passing the parameter to database use below format
input parameter of database = (dateprint == DBNull.value ? null : (DateTime?)convert.toDateTime(dateprint))

VC++ SQL INSERT INTO error

I'm using VC++ (Visual C++) in my system. I'm doing to add student's information in a Access Database which I used SQL Command (INSERT INTO).
this is my codes to add student's information in access database
OleDb::OleDbConnection^ con = gcnew OleDb::OleDbConnection();
OleDb::OleDbCommand^ command = gcnew OleDb::OleDbCommand();
con->ConnectionString::set(conStr);
con->Open();
String^ cmdTxt = "
insert into students(studno, fname, lname,
mname, gender, status, birthday, course, shift, section, homeadd,
provadd, contactnum, nameguard, addguard, contguard, sec1, sub1,
room1, unit1, guro1, sec2, sub2, room2, unit2, guro2, sec3, sub3,
room3, unit3, guro3, sec4, sub4, room4, unit4, guro4, sec5, sub5,
room5, unit5, guro5, sec6, sub6, room6, unit6, guro6, sec7, sub7,
room7, unit7, guro7, sec8, sub8, room8, unit8, guro8, sec9, sub9,
room9, unit9, guro9, sec10, sub10, room10, unit10, guro10)
values('" + studno + "', '" + fname + "', '" + lname + "', '" + mname + "',
'" + sex + "', '" + status + "', '" + birthday + "', '" + course + "',
'" + shift + "', '" + section + "', '" + homeAdd + "', '" + provAdd + "',
'" + contactnum + "', '" + nameGuardian + "', '" + addGuardian + "',
'" + numGuardian + "', '" + se1 + "', '" + sub1 + "', '" + room1 + "',
'" + unit1 + "', '" + guro1 + "', '" + se2 + "', '" + sub2 + "',
'" + room2 + "', '" + unit2 + "', '" + guro2 + "', '" + se3 + "',
'" + sub3 + "', '" + room3 + "', '" + unit3 + "', '" + guro3 + "',
'" + se4 + "', '" + sub4 + "','" + room4 + "', '" + unit4 + "',
'" + guro4 + "', '" + se5 + "', '" + sub5 + "', '" + room5 + "',
'" + unit5 + "', '" + guro5 + "', '" + se6 + "', '" + sub6 + "',
'" + room6 + "', '" + unit6 + "', '" + guro6 + "', '" + se7 + "',
'" + sub7 + "', '" + room7 + "', '" + unit7 + "', '" + guro7 + "',
'" + se8 + "', '" + sub8 + "', '" + room8 + "', '" + unit8 + "',
'" + guro8 + "', '" + se9 + "', '" + sub9 + "', '" + room9 + "',
'" + unit9 + "', '" + guro9 + "', '" + se10 + "', '" + sub10 + "',
'" + room1 + "', '" + unit10 + "', '" + guro10 + "')";
command->Connection::set(con);
command->CommandText::set(cmdTxt);
command->ExecuteNonQuery();
con->Close();
No errors found when i compiled and run it, the problems is when i add student's information shows an error message
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error in INSERT INTO statement.
i checked many times in INSERT INTO, so i changed it
command->CommandText::set("insert into students("
+ "studno,"
+ "fname,"
+ "lname,"
+ "mname,"
+ "gender,"
+ "status,"
+ "birthday,"
+ "course,"
+ "shift)"
+ "values("
+ "'" + studno + "', '"
+ fname + "', '"
+ lname + "', '"
+ mname + "', '"
+ sex + "', '"
+ status + "', '"
+ birthday + "', '"
+ course + "', '"
+ shift + "')");
This code is works 100%, so I add fields,
command->CommandText::set("insert into students("
+ "studno,"
+ "fname,"
+ "lname,"
+ "mname,"
+ "gender,"
+ "status,"
+ "birthday,"
+ "course,"
+ "shift,"
+ "section)"
+ "values("
+ "'" + studno + "', '"
+ fname + "', '"
+ lname + "', '"
+ mname + "', '"
+ sex + "', '"
+ status + "', '"
+ birthday + "', '"
+ course + "', '"
+ shift + "', '"
+ section + "')");
so it gives me an error. -_-
so how to solve this problem
SECTION is a reserved word. If you surround that name with square brackets, Access will understand it is a field name. So change "section)" to "[section])"

Error converting data type varchar to float. C# VS

I have the following SQL statement which is giving me this error.
THIS IS NOT MY CODE!
"Error converting data type varchar to float" But i can't find the error.
I thnk the error is coming from this code:
birthday = (monthComboBox.SelectedItem) + "-" + (dayComboBox.SelectedIndex + 1) + "-" + yearTextBox.Text;
Int32 getIDBack = 0;
string query = "insert into reservation(first_name, last_name, birth_day, gender, phone_number, email_address, number_guest, street_address, apt_suite,city, state, zip_code, room_type, room_floor, room_number, total_bill,payment_type, card_type, card_number,card_exp,card_cvc, arrival_time, leaving_time, check_in, break_fast, lunch, dinner, supply_status, cleaning, towel, s_surprise, food_bill) values('" + firstNameTextBox.Text +
"', '" + lastNameTextBox.Text + "', '" + birthday + "', '" + genderComboBox.SelectedItem + "', '" + phoneNumberTextBox.Text + "', '" + emailTextBox.Text +
"', '" + (qtGuestComboBox.SelectedIndex + 1) + "', '" + addLabel.Text + "', '" + aptTextBox.Text + "', '" + cityTextBox.Text +
"', , '" + zipComboBox.Text + "', '" + roomTypeComboBox.SelectedItem + "', '" + floorComboBox.SelectedItem +
"', '" + roomNComboBox.SelectedItem + "', '" + finalizedTotalAmount + "', '" + paymentType +
"', '" + CardType + "','" + paymentCardNumber + "','" + MM_YY_Of_Card + "','" + CVC_Of_Card + "', '" + dateTimePicker1.Text + "', '" + dateTimePicker2.Text + "','" + checkin +
"', '" + breakfast + "','" + lunch + "','" + dinner + "', '" + foodStatus + "', '" + Convert.ToInt32(cleaning) + "', '" + Convert.ToInt32(towel) + "', '" + Convert.ToInt32(surprise) + "','" + foodBill + "');";
query += "SELECT CAST(scope_identity() AS int)";
SqlConnection connection = new SqlConnection(Hotel_Manager.Properties.Settings.Default.frontend_reservationConnectionString);
SqlCommand query_table = new SqlCommand(query, connection);
try
{
connection.Open();
getIDBack = (Int32)query_table.ExecuteScalar();
string userID = Convert.ToString(getIDBack);
SendSMS(getIDBack);
MetroFramework.MetroMessageBox.Show(this, "Entry successfully inserted into database. " + "\n\n" +
"Provide this unique ID to the customer." + "\n\n" +
"USER UNIQUE ID: " + userID, "Report", MessageBoxButtons.OK, MessageBoxIcon.Question);
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
ComboBoxItemsFromDataBase();
LoadForDataGridView();
reset_frontend();
GetOccupiedRoom();
ReservedRoom();
getChecked();
}
This generally means that one (or more) of the values is a number, rather than a string. If any of the values in a + expression is numeric, then the + is interpreted as addition rather than string concatenation.
One method is to find the offending values (such as finalizedTotalAmount) and then wrap them in conversions: cast(finalizedTotalAmount as varchar(255)).
A better way is to use parameterized queries, where the values are passed in as typed parameters. This is also safer because it prevents SQL injection.

How to use Group By with AND in java codes

Trying to get values from my table but having problem with my snytaxes
there is wrong type with "Group By" usage with "AND"
any help will be apriciated
"SELECT " + C_CINSIYET + " FROM " + TABLE_COMPANYS + " WHERE "
+ C_MARKA + " = '" + companyMarka.getComp_marka() + "'"
+ " AND " + C_FIRMA + " = '"
+ companyMarka.getComp_name() + "' GROUP BY "
+ C_CINSIYET + "AND"+C_FIRMA;

Sending Email within loop in a function

code is building the email but it is only displaying one row when there is six. I got it correct on the text file but I need it to do the same in the email message. I think I got the for next statement in the wrong location. Here is the example of the code that I am having problem with. I do not know how to place the for next statement without interrupting the vbLine
For Each p In query
If p.Contract_No IsNot Nothing Then
ContractNo = p.Contract_No
Else
ContractNo = " "
End If
If p.Vendor_Name IsNot Nothing Then
VenderName = p.Vendor_Name
Else
VenderName = " "
End If
If p.Termination_Date IsNot Nothing Then
TerminationDate = p.Termination_Date
' ReportDateStr = ReportDate.ToString
TerminationDateStr = String.Format("{0:MM/dd/yyyy}", TerminationDate)
Else
TerminationDateStr = " "
End If
If p.Dept_Name IsNot Nothing Then
DeptName = p.Dept_Name
Else
DeptName = " "
End If
If p.Renewal_Option_Desc IsNot Nothing Then
RenewalOption = p.Renewal_Option_Desc
Else
RenewalOption = " "
End If
If p.Contract_Desc IsNot Nothing Then
ContractDesc = p.Contract_Desc
Else
ContractDesc = " "
End If
If p.Contact_Email IsNot Nothing Then
ContactEmail = p.Contact_Email
Else
ContactEmail = "** N/A ** "
End If
' sends email with attachment
EmailMsgBody = "-- TOTAL # OF CONTRACTS WITH FAILSAFE DATE ON " + DateStr + " IS: " + icnt.ToString + vbCrLf +
vbNewLine + " __________________ " +
vbNewLine +
vbNewLine + " *****Contracts**** " +
vbNewLine + " __________________ " +
vbNewLine +
vbNewLine + "Contract#" + " " + "Vender Name" + " " + "Termination Date" + " " + "Dept Name" + " " + "Renewal Option" + " " + "Contract Desc" + " " + "Email Address" +
vbNewLine + "------------" + " " + "-----------------" + " " + "---------------------" + " " + "--------------" + " " + "--------------------" + " " + "-----------------" + " " + "-----------------" +
vbNewLine + ContractNo.PadRight(18) + " " + _
VenderName.PadRight(38) + " " + _
TerminationDateStr.PadRight(26) + " " + _
DeptName.PadRight(27) + " " + _
RenewalOption.PadRight(45) + " " + _
ContractDesc.PadRight(32) + " " + _
ContactEmail.PadRight(11) + " "