I am trying to retrieve a specific id from a table so that I can get the correct record in my database and use that information. Is there anyway where I can get the actually numerical value of the id from the table. I have tried using linq to SQL but it just queries the database and returns the string like this:
Dim id = From data In dbServer.UserTables Where data.Username = user.username And data.userPassword = user.userPassword Select data.UserID
It does work because then if I count id if it is greater than 1 it will work. But trying to send the value on for example in session state will only have "From data In dbServer.UserTables Where data.UserID = user.username And data.userPassword = user.userPassword Select data.UserID" instead of the actual number. How can I do this correctly?
A Linq query will return a set of data, even if there is only one result.
This will get you an integer (possibly a nullable integer, I can't test right now), that you will have to check for value different than 0 (or null, if it's a nullable).
Dim id = (From data In dbServer.UserTables Where data.UserID = user.username And data.userPassword = user.userPassword Select data.UserID).FirstOrDefault()
Related
I am trying to develop a simple method to execute sql queries on my application so I can use native sql for certain things.
This is the method I have:
Session session = getReportCsvMgr().getHibernateSession();
session.beginTransaction();
String sql = String.format("select USER_ID from Users where accountid = 'testaaa'");
Object o = session.createSQLQuery(sql).list();
System.out.println(o.toString());
session.close();
I do not get any errors but somehow the object o is empty and the sysout just prints [].
I debugged and the session works. I tested changing the name of the table and indeed it said "table does not exist". I also tried with and update statement, no errors but it does nothing.
Can anybody tell me what I need to do?
Thanks!
Change the line
Object o = session.createSQLQuery(sql).list();
to:
List<Integer> o = session.createSQLQuery(sql).list();
it the USER_ID is integer or to:
List<String> o = session.createSQLQuery(sql).list();
if the USER_ID is string.
Moreover in a query you have not passed params so you can change:
String sql = String.format("select USER_ID from Users where accountid = 'testaaa'");
to simple:
String sql = "select USER_ID from Users where accountid = 'testaaa'";
Either use .uniqueResult() instead of .list() if it only returns one row or change the return type to List<Object[]>
The code is suppose to check my database to see if there are duplicates of activityName existing if that query runs I am suppose to get an error stating that the activity name is taken else if there isn't any activity name in that database with the same name then the activity name would be inserted into the database. Im suppose to execute the query and get the result as an Integer then use the result in the if and else to see if result>0 in the database
var queryCount= 'SELECT COUNT (activityName) FROM dataEntryTb WHERE activityName = "'+an+'" ';
tx.executeSql(queryCount,[]);
if(queryCount > 0){
navigator.notification.alert("Activity Name Taken");
}else{
Not sure what's the issue exactly but it should work fine. You can consider changing it like
var queryCount= "SELECT 1 FROM dataEntryTb WHERE activityName = '" + an + "'";
Again, consider using parameterized query instead of string concatenation to avoid SQL Injection (if an is coming as user input)
I have the following two queries that take information from the same table
I need to compare the two tables and find all the information that's value is different. Is there any way to do this without having to make a loop?
Dim housepress = (From press In db.PressInfo
Where press.PressName = pressname And press.CustomerID = "House"
Select press).ToList()
Dim curpress = (From press In db.PressInfo
Where press.PressName = pressname And press.CustomerID = Customername
Select press)
I tried using curpress.Except but I get an error that "Local sequence cannot be used in Linq to SQL
I'm trying to check if the field data of my DataTable records is set to '0' or '1'. All my local record is saved into local_ds DataTable. Now, the record that I want check is this: 21a956af-f304-4c72-97cf-1ef08e8719fc
for a better vision I paste here the content of my table (that is also the content of my DataTable local_ds):
How you can see the record that I want check have the field data set to 0. Now I perform the research through this code:
Dim local_data = local_ds.Tables(0).Select(String.Format("GUID = '{0}'", "21a956af-f304-4c72-97cf-1ef08e8719fc"), String.Format("data", 1))
The code above use LINQ to take the result, anyway, I pass the GUID field to search and the field data as 1. This code should be return local_data.length equal to 0 but, instead, return 1 and this is wrong, 'cause I want to check only if the field data is 0 or 1. In this example the result should be local_data.length = 0 'cause in the LINQ query I specified clearly that I want find the record with GUID = x and data = 1.
I already know that this record exists in the database, the local_data variable must help me to recognize which type of valorization the data field have.
So, what I did wrong?
No, in the code above, you are not using LINQ.
DataTable.Select is a method available starting from the 1.1 version of NET Framework and exists in four possible overloads.
The one you are using is the one that takes, as first parameter, the WHERE condition and, as second parameter, the SORT order. So you are not really passing a condition WHERE .... AND Data = 1 but the Data=1 it is interpreted as a sort order of some kind.
The correct string for the WHERE parameter should be
Dim where as String = String.Format("GUID = '{0}' AND Data = {1}", _
"21a956af-f304-4c72-97cf-1ef08e8719fc", 1)
Dim local_data = local_ds.Tables(0).Select(where)
Dim newRow As DataRow = dt.NewRow
newRow.Item(1) = Student_IDTextBox.Text
newRow.Item(2) = Student_NameTextBox.Text
newRow.Item(3) = Date_of_BirthDateTimePicker.Text
newRow.Item(4) = AddressTextBox.Text
newRow.Item(5) = E_mailTextBox.Text
newRow.Item(6) = AllergiesTextBox.Text
newRow.Item(7) = Emergency_Contact_NumberTextBox.Text
dt.Rows.Add(newRow)
The line
newRow.Item(2) = Student_NameTextBox.Text
gives a error saying:
"String was not recognized as a valid DateTime. Couldn't store <> in Date of Birth Column. Expected type is DateTime."
But I checked the database the second row is the Student name Field.
I think the code is starting to input the student name into the student ID field in the database, The Student Id is the primary key coe this be why it isn't storing the student_IDtextbox data in its specified field?
I realy need help with this my project is due in about 9 days!
the columns index in DataRow class, Similar all indexs in vb.net, its zero-based.
first column is zero, second one and ect.