Restrict data to logged in user asp.net [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am creating a medication ordering website as part on an assignment. I have a link table that contains the users id and medication id.
When a user is logged in I want them to see their medication only
SELECT [MedicineId] FROM [Prescription] WHERE ([PatientId] = #PatientId)
I have not created a log in yet because I dont want to log in every time im testing.
[![tables][2]][2]

You can use Session for a sample patient.
Session["PatientId"] = 1453;
var SqlQuery = "SELECT [MedicineId] FROM [Prescription] WHERE [PatientId] =" + Session["PatientId"].ToString();
With SQL Parameters
var SqlQuery = "SELECT [MedicineId] FROM [Prescription] WHERE [PatientId] = #PatientId";
var connection = new SqlConnection(/* your DB connection */);
var command = new SqlCommand(SqlQuery, connection);
command.Parameters.AddWithValue(
"PatientId", Session["PatientId"].ToString());

Related

Set SQL WHERE value using an element from a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a variable with a list on it and I need to use its value for my find option. I get an error when I set my id_user to id_u.
Here is the list
id_u = user_key[0]
This is my SELECT and WHERE
find = ("SELECT * FROM hashtags WHERE id_user=id_u")
You have to concatenate SELECT string with variable value.
Try like this:
id_u = user_key[0]
find = ("SELECT * FROM hashtags WHERE id_user=" + id_u)

Why am I getting an exception telling me that I need to declare a scalar variable here (referencing a parameter in a query)? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've got a parameterized query which I build up based on selection criteria the user chooses on a web page. For example, here is an example query string (completeQuery) that is built:
SELECT M.MovieTitle, M.IMDBRating, M.MPAARating, M.YearReleased, M.Minutes
FROM dbo.MOVIES_MAIN M
LEFT JOIN GENRES_MOVIES_M2M G ON M.MovieId = G.MovieId
WHERE M.IMDBRating >= #IMDBMinRating
AND M.YearReleased BETWEEN #EarliestYear AND #LatestYear
AND G.GENRES IN (Biography, Documentary, Drama, History, Music, Mystery, Western )
AND M.MPAARating IN (PG )
ORDER BY M.IMDBRating DESC, M.YearReleased DESC
I then attempt to assign the result of the query (contained in the "completeQuery" string) to a GridView like so:
try
{
using (SqlConnection connection = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(completeQuery, connection))
{
cmd.Parameters.AddWithValue("#IMDMinRating", _imdbThreshold);
cmd.Parameters.AddWithValue("#EarliestYear", _yearBegin);
cmd.Parameters.AddWithValue("#LatestYear", _yearEnd);
SqlDataAdapter dAdapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dAdapter.Fill(ds);
GridView1.DataSource = ds.Tables[0];
connection.Close();
}
}
}
catch (Exception ex)
{
string s = ex.Message;
}
An exception is thrown on the following line:
GridView1.DataSource = ds.Tables[0];
The exception message is Must declare the scalar variable "#IMDBMinRating".
Since that is the first parameter added, I assume it would also complain about the other two parameters.
Why is it not seeing/accepting #IMDMinRating?
Your SQL query says IMDB and your C# parameter name says IMD
WHERE M.IMDBRating >= #IMDBMinRating
^
cmd.Parameters.AddWithValue("#IMDMinRating", _imdbThreshold);
^
I assume it would also complain about the other two parameters.
The other two parameters don't have typos.. And of course if the error had been "No value supplied for #IMDBMinRating. Query doesn't use supplied parameter named '#IMDMinRating'" you probably would have realized immediately!
It's one of those things you just have to chalk up to experience and double check next time, when SQLServer says a parameter in your query doesn't have a value it's usually the case (and easy to miss)..
Unrelated to your issue, but you should read the advice in Joel's blog about AddWithValue

How to create a data frame from a text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am having trouble creating a data frame with the following data:
Force (N) microstrain1 microstrain2 microstrain3 microstrain4 microstrain5
24.838 9.689 -20.299 19.785 15.601 -7.681
49.691 22.610 -40.797 41.304 32.200 -15.332
75.309 33.357 -61.678 62.512 48.726 -22.422
97.227 41.944 -80.524 81.011 62.266 -30.228
121.641 52.692 -100.775 100.703 77.248 -36.884
Every time I try to use a delimiter I get the following message:
/Users/macbookpro/PycharmProjects/Projects/Lab_3/Bending.py:5: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
df1 = pd.read_csv('MEE322-thurs_1040_group1_9.5cm.txt',delimiter=' ')
Did you try the python engine instead of the c engine?:
df1 = pd.read_csv('MEE322-thurs_1040_group1_9.5cm.txt', delimiter=' ', engine='python')

ASPNET MVC WITH EF- SUM & GROUP BY [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I tried to convert that SQL Query to Link but it does not work.
SELECT
SUM([Quantity]) as qt
,[ArticleID]
FROM [DB].[dbo].[Location]
Group by ArticleID
Order by qt
List <Location> articles = contexteEF.Location.GroupBy(l => l.ArticleID).Select(a => new { qt = a.Sum(b => b.Quantity), ArticleID = a.Key }).OrderByDescending(a => a.qt).ToList();
Can you help me! Please!
Thanks
You are trying to cast a list of dynamic objects to a List<Location>
I assume that Location has the properties qt and ArticleId? In that case, don't create dynamic objects, create Locations. Code not tested, but something like:
List <Location> articles = contexteEF.Location.GroupBy(l => l.ArticleID).Select(a => new Location() { qt = a.Sum(b => b.Quantity), ArticleID = a.Key }).OrderByDescending(a => a.qt).ToList();

Update SQL Records in Drupal [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I need to update 50K records by changing the status of comments (turning read & read/write into disabled) and want to make sure my SQL statement is correct:
$query = "UPDATE node.nid AS nid, node.comment AS node_comment, node.type AS node_type
SET node.comment = '0'
WHERE (node.type in ('article', 'blog', 'event'))
AND (node.comment in ('1','2'))";
$total = 0;
$count = 0;
while ($query_result = db_query($query)){
$count++;
$total++;
if($count>200){
$count = 0;
sleep(300);
}
}
echo "Updated records:" . $total;
I added a periodic pause in there so it doesn't kill the server. Does this look ok?
I changed the UPDATE to just node node and that worked.