SQLCMD is not executed through asp.net application - vb.net

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()

Related

Sending object(json) using vba

Im trying to send json object in Outlook using vba. Here is my code:
Dim Msg As Outlook.MeetingItem
Set Msg = Item
Set recips = Msg.Recipients
Dim regEx As New RegExp
regEx.Pattern = "^\w+\s\w+,\sI351$"
Dim URL As String
URL = "https://webhook.site/55759d1a-7892-4c20-8d15-3b8b7f1bf3b3"
For Each recip In recips
If regEx.Test(recip.AddressEntry) And recip.AddressEntry <> "Application Management Linux1, I351" Then
Dim convertedJson As Object
Set convertedJson = JsonConverter.ParseJson("{""fields"": 123}")
Set xhr = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xhr.Open "POST", URL, False
xhr.setRequestHeader "Content-Type", "application/json"
xhr.Send (convertedJson)
End If
Next
If I send just plane text it works well but i can't send convertedJson. Is it possible to send an object?
UPDATE
I can't even do Debug.Print convertedJson
I was tormented by these libraries in the end I did a very terrible thing
Dim flds, prt, id, smry, descrp, issu, name, lfbrkt, rtbrkt, cma, dbdots, jsTest, issuName As String
flds = "'fields'"
prt = "'project'"
id = "'id'"
smry = "'summary'"
descrp = "'description'"
issu = "'issuetype'"
issuName = "'Improvement'"
name = "'name'"
lfbrkt = "{"
rtbrkt = "}"
cma = ","
dbdots = ":"
jsTest = lfbrkt + flds + dbdots + " " + lfbrkt + vbCrLf + vbTab + prt + dbdots + " " + lfbrkt + vbCrLf + vbTab + vbTab + id + dbdots + " " + "30611" + vbCrLf + vbTab + rtbrkt + cma + vbCrLf + vbTab + smry + dbdots + " " + "'" + CStr(Msg.Subject) + "'" + cma + vbCrLf + vbTab + descrp + dbdots + " " + "'" + CStr(Msg.Body) + "'" + cma + vbCrLf + vbTab + issu + dbdots + " " + lfbrkt + vbCrLf + vbTab + vbTab + name + dbdots + " " + issuName + vbCrLf + vbTab + rtbrkt + vbCrLf + rtbrkt + rtbrkt
And I got this

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

vb.net syntax error in INSERT INTO statement

I get syntax error in INSERT INTO statement when trying to insert into access db. The snippet where the error occurs is,
If (checkBox.IsChecked) Then
cmd.CommandText = "INSERT INTO Participants([Full Name],[From],[Gender],[Category],[Event],[Weight],[DOB],[Age]) VALUES(" + textBox.Text + "," + textBox_Copy2.Text + "," + gender + "," + comboBox.SelectedItem.ToString + "," + "Kata" + "," + textBox_Copy.Text + "," + dp.Text + "," + textBox_Copy1.Text
cmd.ExecuteNonQuery()
End If
If (checkBox_Copy2.IsChecked) Then
cmd.CommandText = "INSERT INTO Participants([Full Name],[From],[Gender],[Category],[Event],[Weight],[DOB],[Age]) VALUES(" + textBox.Text + "," + textBox_Copy2.Text + "," + gender + "," + comboBox.SelectedItem.ToString + "," + "Kumite" + "," + textBox_Copy.Text + "," + dp.Text + "," + textBox_Copy1.Text
cmd.ExecuteNonQuery()
End If
If (checkBox_Copy1.IsChecked) Then
cmd.CommandText = "INSERT INTO Participants([Full Name],[From],[Gender],[Category],[Event],[Weight],[DOB],[Age]) VALUES(" + textBox.Text + "," + textBox_Copy2.Text + "," + gender + "," + comboBox.SelectedItem.ToString + "," + "Team Kata" + "," + textBox_Copy.Text + "," + dp.Text + "," + textBox_Copy1.Text
cmd.ExecuteNonQuery()
End If
If (checkBox_Copy.IsChecked) Then
cmd.CommandText = "INSERT INTO Participants([Full Name],[From],[Gender],[Category],[Event],[Weight],[DOB],[Age]) VALUES(" + textBox.Text + "," + textBox_Copy2.Text + "," + gender + "," + comboBox.SelectedItem.ToString + "," + "Team Kumite" + "," + textBox_Copy.Text + "," + dp.Text + "," + textBox_Copy1.Text
cmd.ExecuteNonQuery()
End If
For text fields you have to enclose the value in the VALUES clause in quotes. Note the single quotes in this example
"INSERT INTO Participants([Full Name]) VALUES ('" + textBox.Text + "')"
However you would be much better off using parameters instead of putting the values right in the SQL statement. See this
https://msdn.microsoft.com/en-us/library/tyy0sz6b(v=vs.110).aspx

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