get data after union max and min sql statement - sql

Is there any way to combine these sql statements in one statement:
Dim s As String =
"SELECT byu,MAX(atttime) AS attime FROM att
WHERE pno='" + DataGridView1.Rows(i).Cells(0).Value.ToString + "'
and attdate ='" + curdate + "' and atttime>='" + mxtime + "'
and atttime<='" + mxtime2 + "' "
Dim xmd As New SqlCommand(s, con)
Dim dr As SqlDataReader = xmd.ExecuteReader
If dr.Read Then
DataGridView1.Rows(i).Cells(7).Value = dr("attime")
DataGridView1.Rows(i).Cells(14).Value = dr("byu")
End If
dr.close
Dim s2 As String =
"SELECT byu, MIN(atttime) AS attime FROM att
WHERE pno='" + DataGridView1.Rows(i).Cells(0).Value.ToString + "'
and attdate ='" + curdate + "' and atttime>='" + mintime + "'
and atttime<='" + mintime2 + "' "
Dim xmd2 As New SqlCommand(s2, con)
Dim dr2 As SqlDataReader = xmd2.ExecuteReader
If dr2.Read Then
DataGridView1.Rows(i).Cells(4).Value = dr2("attime")
DataGridView1.Rows(i).Cells(15).Value = dr2("byu")
End If
dr2.Close()
the table att has data as:
pno attdate atttime byu
2 2015/01/02 07:05:02 0
2 2015/01/02 07:07:02 1
2 2015/01/02 18:08:11 0
2 2015/01/02 19:15:02 1
i was trying since morning and didn't come up with something, the problem that when i union the 2 sql statement i couldn't get the "byu" of MAX(atttime) and also the "byu" of MIN(atttime) via SqlDataReader. I almost red all the questions in the site that related to this and nothing worked for me so far.the result of above code is:
2 2015/01/02 07:05:02 0
2 2015/01/02 19:15:02 1
please help, thanks.

You can join you queries like this and execute them as a batch statement
Dim s As String =
"SELECT byu as byu1,MAX(atttime) AS attime1 FROM att
WHERE pno='" + DataGridView1.Rows(i).Cells(0).Value.ToString + "'
and attdate ='" + curdate + "' and atttime>='" + mxtime + "'
and atttime<='" + mxtime2 + "' group by byu ;" +
"SELECT byu as byu2,MIN(atttime) AS attime2 FROM att
WHERE pno='" + DataGridView1.Rows(i).Cells(0).Value.ToString + "'
and attdate ='" + curdate + "' and atttime>='" + mintime + "'
and atttime<='" + mintime2 + "' group by byu ;"
This is the only idea i dont know how many columns you are getting from database you have to set your cells() value respective of column order which you are getting from database
Dim xmd As New SqlCommand(s, con)
Dim dr As SqlDataReader = xmd.ExecuteReader
If dr.Read Then
DataGridView1.Rows(i).Cells(7).Value = dr("attime1")
DataGridView1.Rows(i).Cells(14).Value = dr("byu1")
DataGridView1.Rows(i).Cells(4).Value = dr("attime2")
DataGridView1.Rows(i).Cells(15).Value = dr("byu2")
End If

Related

System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression.' Grid view update

I am working on a grid view update but System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression error. Please someone help.
protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtFName2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtFName");
TextBox txtDate2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtSM");
TextBox txtCaseType2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtCaseType");
TextBox txtFileno2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtFileno");
TextBox txtCustName2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtCustName");
TextBox txtAddress2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtCustName");
TextBox txtConno2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtConno");
TextBox txtPlotarea2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtPlotarea");
TextBox txtPlotRate2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtPlotRate");
TextBox txtconstarea2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtconstarea");
TextBox txtConstFloor2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtConstFloor");
TextBox txtconstrate2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtconstrate");
TextBox txtPlotvalue2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtPlotvalue");
TextBox txtconstvalue2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtconstvalue");
TextBox txttotalvalue2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txttotalvalue");
TextBox txtRemarks2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtRemarks");
**TextBox txtDatee2 = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtDate2");**
string da = txtDatee2.Text.ToString();
DateTime dt =
DateTime.ParseExact(da, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
string dateshort = dt.ToShortDateString();
string ID = ResultGridView.DataKeys[e.RowIndex].Values[0].ToString();
cmd.Connection = conn;
cmd.CommandText = "UPDATE Final SET Finance ='" + txtFName2.Text + "' ,SM ='" + txtDate2.Text + "',Case_Type ='" + txtCaseType2.Text + "',File_no ='" + txtFileno2.Text + "',Cust_Name ='" + txtCustName2.Text + "' ,Address ='" + txtAddress2.Text + "',Con_no ='" + txtConno2.Text + "' ,Plot_area ='" + txtPlotarea2.Text + "' ,Plot_Rate ='" + txtPlotRate2.Text + "' ,const_area ='" + txtconstarea2.Text + "' ,Const_Floor ='" + txtConstFloor2.Text + "' ,const_rate ='" + txtconstrate2.Text + "' ,Plot_value ='" + txtPlotvalue2.Text + "' ,const_value ='" + txtConno2.Text + "' ,total_value ='" + txttotalvalue2.Text + "' ,Remarks ='" + txtRemarks2.Text + "'**,Date2 = '"+ dateshort +"'** WHERE ID=" + ID + "";
conn.Open();
cmd.ExecuteNonQuery();
ResultGridView.EditIndex = -1;
FillVendorGrid();
conn.Close();
}
At least, first text expressions for date values should be formatted as to the ISO sequence:
string dateshort = dt.ToString("yyyy'/'MM'/'dd");
second, in Access, these must be wrapped in octothorpes:
"', Date2 = #" + dateshort + "# WHERE ID="
This must be modified for other datetime fields as well.
Or, do your self a big favour and turn to call a parameterised query.

My VB.NET loop code doesn't work. Any Ideas?

I'm making a script to help me out filling in a MySQL table. It basically loops through every year group and set and makes a class for their subject. For Example : Year Group 8, Set 4, English would be 84En etc etc. I made it so it loops through an ArrayList and changes some variables but I can't seem to get it to work.
I don't really know what to try to be honest because I've checked over it and I don't know if I'm blind but I don't see any errors.
Dim subjects As New ArrayList
subjects.Add("English")
subjects.Add("Welsh")
subjects.Add("French")
subjects.Add("Maths")
subjects.Add("Science")
subjects.Add("History")
subjects.Add("Geography")
subjects.Add("Pe")
subjects.Add("Re")
subjects.Add("Tutorial")
subjects.Add("Music")
subjects.Add("Drama")
subjects.Add("Technology")
subjects.Add("It")
subjects.Add("Art")
Dim upsubjects As New ArrayList
upsubjects.Add("English")
upsubjects.Add("Welsh")
upsubjects.Add("Physics")
upsubjects.Add("Biology")
upsubjects.Add("Chemistry")
upsubjects.Add("French")
upsubjects.Add("Maths")
upsubjects.Add("Science")
upsubjects.Add("History")
upsubjects.Add("Music")
upsubjects.Add("Technology")
upsubjects.Add("It")
Dim year As Integer = 7
Dim setting As Integer = 8
Dim done As Boolean = False
Dim cmd As MySqlCommand
While done = False
For Each subject As String In subjects
If year = 9 And setting = 8 Then
cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
cmd.ExecuteNonQuery()
setting = 1
End If
If setting = 8 And Not year = 9 Then
cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
cmd.ExecuteNonQuery()
setting = 1
year = year + 1
Else
cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
cmd.ExecuteNonQuery()
setting = setting + 1
End If
Next
For Each subject As String In upsubjects
If year = 11 And setting = 8 Then
cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
cmd.ExecuteNonQuery()
setting = 1
done = True
End If
If setting = 8 And Not year = 11 Then
cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
cmd.ExecuteNonQuery()
setting = 1
year = year + 1
Else
cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
cmd.ExecuteNonQuery()
setting = setting + 1
End If
Next
End While
I'm not getting any error messages, the form just loads (when the code is supposed to execute) and does nothing. Refreshed my MySQL database and nothing still. Any ideas on how to fix this??
If you're concatenating numeric values setting and year into your query string, don't you need to convert them to strings before you do so, with something like CStr()?
Not getting an error suggests to me that the code isn't being executed, but as others said in the comments, you can soon add a 'Stop' to check that.
Also, have a read up on "Database normalisation", with regard to combining various values into a single column in the table, think about how you're going to retrieve that information and how much easier it will be if you keep it separate.

VB 2010 INSERT INTO syntax error

Hello Good Afternoon I have a program in VB.Net that will Input data
from textboxes into Access Database here is sample image
This is the code I am using and it gives me an error
m = TextBox1.Text
b = "'" + TextBox2.Text + "'"
x = "'" + TextBox3.Text + "'"
d = TextBox4.Text
n = "'" + TextBox5.Text + "'"
Dim s2 As String
s2 = "insert into users2 ( num , name1 , pass , add , phone ) " & " values ( " + m + " , " + n + " , " + b + " , " + x + " , " + d + " ) "
Dim cmd2 As New OleDbCommand(s2, con)
cmd2.ExecuteNonQuery()
Looking forward that someone will enlighten my problem since its im starting to program.
TYSM for future help
You need to encapsulate values you want to insert into ''
. "values('" + m + "', '" + ...
2 I don't understand & operator between two parts of query in the beginning

DataGridView sorts dates in wrong order

I have a DataGridView and a searchbox where I can search for different dates in a certain column. Now since the date is formated as string he will give me the wrong order:
I type in 20 and get:
20.10.2014,
22.09.2014,
24.11.2014
and so on. I have read another thread here about this problem but the solutions didn't help me. My SQL statement looks like following:
DataTable datTable = new DataTable();
sqlCmd = new SqlCommand("SELECT ["+form1.timeBox.Text+ "] FROM [" + form1.getTableName() + "] WHERE convert(varchar(10),[" + form1.getTimeCol() + "],104) >= '" + form1.getFromDate().Trim() + "' ORDER BY convert(varchar(10),[" + form1.getTimeCol() + "],104) ASC", connection);
sqlDatAdapter = new SqlDataAdapter(sqlCmd.CommandText, connection);
sqlDatAdapter.Fill(datTable);
form1.setDataGrid = datTable;
and
form1.getFromDate()
is the function which grabs the entered string from the Textbox to search for. I tried to cast and convert to datetime and so on but it gets still shown in the wrong order. Can anyone help?
you are ordering by the formatted column; there is no need to do so and that is the part creating your problem.
i'm against string concatenation to build sql commands but your code should be rewritten as follows:
sqlCmd = new SqlCommand("SELECT ["+form1.timeBox.Text+ "] FROM [" + form1.getTableName() + "] WHERE convert(varchar(10),[" + form1.getTimeCol() + "],104) >= '" + form1.getFromDate().Trim() + "' ORDER BY " + form1.getTimeCol() + " ASC", connection);
nstead of using '>=' use 'Like' operator with '%' character at the end of your "form1.getFromDate().Trim()", which will give you the required result.
Using 'Like' your query will look like:
sqlCmd = new SqlCommand("SELECT ["+form1.timeBox.Text+ "] FROM [" + form1.getTableName() + "] WHERE convert(varchar(10),[" + form1.getTimeCol() + "],104) Like '" + form1.getFromDate().Trim() + "%' ORDER BY convert(varchar(10),[" + form1.getTimeCol() + "],104) ASC", connection);

How to search int or string in all fields

SqlDataAdapter da =
new SqlDataAdapter("SELECT *
FROM Patient
Where Registration_Id = '" + textBox1.Text + "'
OR Patient_Name = '" + textBox1.Text + "'", cn);
How to search int or string in all fields?
Edit code:
if (comboBox1.Text == "Registration_Id")
{
da = new SqlDataAdapter("SELECT *
FROM Patient
Where Registration_Id = '" + textBox1.Text + "'", cn);
}
else if (comboBox1.Text == "Patient_Name")
{
da = new SqlDataAdapter("SELECT *
FROM Patient
Where Patient_Name = '" + textBox1.Text + "'", cn);
}
For one that method can be subject to SQL injection attack so you need to sanitize it. Otherwise one solution (after you check for attacks) is:
SELECT *
FROM Patient
Where column1 like "'%" + textBox1.Text + "%'"
or column2 like "'%" + textBox1.Text + "%'"
......
or columnn like "'%" + textBox1.Text + "%'"
This can be made simpler by the fact that if they don't know the id of the item then don't check that column. Otherwise have a dropdown that chooses which column they are searching by.