Showing posts with label assignment. Show all posts
Showing posts with label assignment. Show all posts

Thursday, February 16, 2012

Amateur SQL formation error

Hi I have the error:

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Line 29: command.CommandText = "UPDATE Items SET Quantityavailable = '+TextBox1.Text+' + Quantityavailable.ToString()"; "INS...
 
Refering to my poor coding:
 
private bool ExecuteUpdate(int quantity)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "CustomString";

con.Open();

SqlCommand command = new SqlCommand();
command.Connection = con;
command.CommandText = "UPDATE Items SET Quantityavailable = '+TextBox1.Text+' + Quantityavailable.ToString()"; "INSERT Transactions SET Usersname = '<%# System.Web.HttpContext.Current.User.Identity.Name %>'"; "INSERT Transactions SET Itemid = '@.ProID'"; "INSERT Transactions SET itemname = '@.ItemName'"; "INSERT Transactions SET Date = '<%# DateTime.Now %>'";
command.ExecuteNonQuery();

con.Close();
}

protected void Button2_Click(object sender, EventArgs e)
{
TextBox tb = FormView.FindControl("TextBox1") as TextBox;
ExecuteUpdate( Int32.Parse(tb.Text) );
}

 
Can someone tell me what I've done wrong?
Thanks,
Jon 

What is Quantityavailable.ToString? I don't see that in the code.

Should iot just be

UPDATE Items SET Quantityavailable = '+TextBox1.Text;

|||

Your update statement doesn't specify which item in the items table you want to update.

You have multiple inserts into the transactions table, and you are using the UPDATE syntax for each.

You have quotation problems.

You can't use the databinding syntax in code <% ... %>.

You have parameters defined (@.ProID, @.ItemName) in the SQL, but never declared or set a value for them (And you have them inside quotes).

Try something like (Sorry, not all that good at C#, but it would look something like this):

command.CommandText = "UPDATE Items SET QuantityAvailable = @.qty WHERE ItemID=@.ItemID; INSERT INTO Transactions(Usersname,ItemID,ItemName,[Date]) VALUES (@.User,@.ItemID,@.ItemName,getdate())";

command.Parameters.Add("@.qty",sqldbtypes.Integer).Value = quantity;

command.Parameters.Add("@.User",sqldbtypes.Nvarchar).Value = System.Web.HttpContext.Current.User.Identity.Name;

command.Parameters.Add("@.ItemID",sqldbtypes.Integer).Value = ?;

command.Parameters.Add("@.ItemName",sqldbtypes.NVarchar).Value= ?;

|||

Hi thanks for your response..

The thing is, I am trying to make one table update (Items - Quantityavailable) with the value typed into the textbox, at the same time as another has data inserted (Transactions).


The data to be inserted is databound to the formview of the page - thats why I didnt define the parameters (@....@....) I thought they would just get lifted from the formview as they are defined within the formview. Is there a way to do this?

I have modified my code to this:

SqlConnection con = new SqlConnection();
con.ConnectionString = "CustomString";

con.Open();

SqlCommand command = new SqlCommand();
command.Connection = con;
command.CommandText = "UPDATE Items SET Quantityavailable = +TextBox1.Text' ";
"INSERT INTO Transactions(Usersname,ItemID,ItemName,[Date]) VALUES (@.User,@.ItemID,@.ItemName,getdate())";
command.Parameters.Add("@.User",sqldbtypes.Nvarchar).Value = System.Web.HttpContext.Current.User.Identity.Name;
command.Parameters.Add("@.ItemID",sqldbtypes.Integer).Value = ?;
command.Parameters.Add("@.ItemName",sqldbtypes.NVarchar).Value= ?;

command.ExecuteNonQuery();

con.Close();

Thanks,

Jon

|||

Should be

"UPDATE Items SET Quantityavailable = " +TextBox1.Text + ";"

|||

mcp111:

Should be

"UPDATE Items SET Quantityavailable = " +TextBox1.Text + ";"

Rather, I would recommend using parameterized queries to prevent SQL Injection attacks.

"UPDATE Items SET Quantityavailable = @.Qty"

Sunday, February 12, 2012

Alternatives to SQL *Plus

Hi,

I have an SQL assignment to do and at my school we use SQL *Plus there
however I don't have Oracle at home, where I would like to do the work ,so I
was wondering whats the easiest way to get an SQL environment up so I can
code in that then just paste it into SQL *Plus later.

I don't really want to install Oracle on my home pc and I was wondering if
there are other IDE's for SQl that would fit my need for this.

I discovered an instant SQL *Plus client that sounded really promising but
when i unpacked it, it was just a load of dll's so I think it wasn't what I
thought it was.

So does anyone know of anything that might be able to help me out here?

Any advice much appreciated!

Thanks
--
AntTry asking in an Oracle group. This is a MS SQL Server group.

--
David Portas
SQL Server MVP
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1108563340.314230.94240@.f14g2000cwb.googlegro ups.com...
> Try asking in an Oracle group. This is a MS SQL Server group.
> --
> David Portas
> SQL Server MVP

Done!

But this newsgroup is much busier so I figured it would be more likely
someone here would be able to help me out.

Thursday, February 9, 2012

Alternative to GROUP BY

I have another question from my Uni assignment! I'm sure it's the way the questions are written that I'm having difficulty with! :(

==============================
Write a query with the same meaning as the following query, but does not use a GROUP BY clause.

SELECT EventId, MIN (ElapsedTime), MAX (ElapsedTime)
FROM Results
GROUP BY EventId
=============================

There are approx 8 results per event, each with an elapsed time - it is results of swimming races.

I thought you had to use a GROUP BY with aggregate functions? I thought about temporary tables, but that doesn't eliminate the group by.

Thanks!

Nerddettewhat a wonderful question!!

have you tried a three-way self join with two correlated subqueries?

it will give the right answer, but it's not very efficient!!|||Tell your teacher he/she is an idiot and the query works perfectly well the way it's written. (grin)|||derrick, it was an intellectual exercise

whatsamatter, you couldn't do it? :p|||No, I couldn't do it because it's stupid. :) I'm allergic to doing something stupid for intellectual reasons.|||whatsamatter, you couldn't do it? :p

* puts her hand up * Um... yes? I still can't do it!

Any ideas, any commands that I can look up the syntax for? I think it might involve a subquery in the SELECT list, but I'm not sure. :(

Thanks.

Nerddette|||nerddette, i gave you my idea in post #2

i tested it and it works|||Without knowing which version of what database engine you are using, it is kind of tough to forumuate an answer. Assuming that your particular engine supports at least the basic SQL-92 syntax, you could do a SELECT DISTINCT to get the grouping done for you (it does the same thing, but doesn't require the GROUP BY clause). Once you've got that, you can probably use correlated sub-queries to get the Min and Max values for that particular EventId.

If you are looking for "pre-cooked" SQL, ready to submit for a grade, you will probably wait a long time here. I'm always happy to help somebody out, but I'm allergic to doing other people's homework!

I think that Rudy is just being perverse with the three-way self-join. I can see how it would work (although I wouldn't want to actually watch it), but I can think of at least 1e2 (an inside joke) easier ways to do it!

-PatP|||If you are looking for "pre-cooked" SQL, ready to submit for a grade, you will probably wait a long time here. I'm always happy to help somebody out, but I'm allergic to doing other people's homework!

Thanks for your help. I'm not looking for "pre-cooked" answers, but I understand your concerns as I get students at Uni wanting the same from me. :)

Nerddette|||I think that Rudy is just being perverse with the three-way self-join.

nope

it was the only way i could think of to do it

i do not, however, see how your way would work, pat

a correlated subquery can be used to restrict which value of ElapsedTime is chosen, but how do you get both min and max selected without a self-join?|||Folks,

How about following

select distinct EventId
,min_elapsed=(select min(b.ElapsedTime)
from Results c
where b.EventId=a.EventId)
,max_elapsed=(select max(c.ElapsedTime)
from Results c
where c.EventId=a.EventId)
from Results a|||a correlated subquery can be used to restrict which value of ElapsedTime is chosen, but how do you get both min and max selected without a self-join?Sushant is one character away from what I intended.

-PatP|||sushant, that's eventually where my self-join was headed

pat's right, i was being sneaky (i am trying to learn from a master)

i wasn't just going to plop orthogonality into the discussion without some sort of leadup

by the way you have a syntax error, or was that on purpose too? (just kidding)|||i wasn't just going to plop orthogonality into the discussion without some sort of leadupAwwww, why not ?!?!

Sneaky? Is there somebody being sneaky around here? Who, where, how ? Why am I always the last one to find out about these things ?!?!

-PatP|||speaking of orthogonality, i went in search of a few good links, and look what i found:

RelationalWeenie (http://c2.com/cgi/wiki?RelationalWeenie)

two things of interest there: the entire wiki looks like a goldmine for computer related stuff, and look, it's another site with a two-character domain name -- do you have any idea what that domain name might be worth on the open market? and there i was, at the dawn of the web, when there were plenty of these names available, and i never bothered to snap a few up...