How to use Group By with AND in java codes - sql

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;

Related

SQLCMD is not executed through asp.net application

I have asp.net application to upload database on a remote server using SQLCMD, but the command is not executed. If I execute it in cmd directly it works fine. Here is the code
Dim output
Dim result As String = ""
p.StartInfo.UseShellExecute = False
p.StartInfo.CreateNoWindow = True
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.Verb = "runas"
p.StartInfo.FileName = "cmd.exe"
'p.StartInfo.Arguments = "/c sqlcmd -S " + serverIP + " -U " + sa + " -P " + P#ssw0rd + " -Q" + Chr(34) + " RESTORE DATABASE " + txtDatabaseName.Text + " FROM DISK=" + Chr(39) + dbPath + Chr(39) + " with move " + Chr(39) + oldmdf + Chr(39) + " to 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\" + mdf + Chr(39) + ", move " + Chr(39) + oldLog + Chr(39) + " to 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\" + txtDatabaseName.Text + "_log.ldf'" + " CREATE Login " + txtDatabaseUser.Text + " WITH PASSWORD = " + Chr(39) + txtDatabasePassword.Text + Chr(39) + ";" + Chr(34)
p.StartInfo.Arguments = "/c sqlcmd -S " + ddlDatabaseServerURL.SelectedValue + " -U " + txtDatabaseServerUsername.Text + " -P " + txtDatabaseServerPassword.Text + " -Q" + Chr(34) + " RESTORE DATABASE " + txtDatabaseName.Text + " FROM DISK=" + Chr(39) + dbPath + Chr(39) + " with move " + Chr(39) + oldmdf + Chr(39) + " to 'F:\MSSQL12.MSSQLSERVER\MSSQL\DATA\" + mdf + Chr(39) + ", move " + Chr(39) + oldLog + Chr(39) + " to 'F:\MSSQL12.MSSQLSERVER\MSSQL\DATA\" + txtDatabaseName.Text + "_log.ldf'" + " CREATE Login " + txtDatabaseUser.Text + " WITH PASSWORD = " + Chr(39) + txtDatabasePassword.Text + Chr(39) + ";" + Chr(34)
Dim strFile As String = "C:\inetpub\vhost\Endpoint_Website\Endpoint_EB\mylog.txt"
Dim fileExists As Boolean = File.Exists(strFile)
Using sw As New StreamWriter(File.Open(strFile, FileMode.OpenOrCreate))
sw.WriteLine(
IIf(fileExists, p.StartInfo.Arguments, Nothing))
End Using
p.Start()
output = p.StandardOutput.ReadToEnd()

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])"

Ordering subsections of case statement

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

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.

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) + " "