|
|
I'm trying to send an update request to existing database in my local machine's sql server instance. Here's the code where I'm formatting the update request and also setting parameters for it:
<pre>
updCmd = new SqlCommand("UPDATE Networks(ID, NetworkName, DomainName, LastUpdatedBy, LastUpdatedAt)" +
" VALUES(@ID,@netName,@domName,@lstUpdtdBy,@lstUpdtdAt) WHERE ID=" + netWorkIDInt, myConnection);
updCmd.Parameters.AddWithValue("@ID", netWorkIDInt);
updCmd.Parameters.AddWithValue("@netName",netName);
updCmd.Parameters.AddWithValue("@domName",domName);
//updCmd.Parameters.AddWithValue("@crtdBy", networksCreatedBy);
updCmd.Parameters.AddWithValue("@lstUpdtdBy",lstUpdtdBy);
//updCmd.Parameters.AddWithValue("@crtdAt",networksCreatedAt);
updCmd.Parameters.AddWithValue("@lstUpdtdAt",DateTime.Now);
</pre>
I've tried more than a few different formatting variations but nothing so far has cleared up the error mentioned in the subject, which is what causes the application to consistently fail.
I would appreciate any help of suggestions, in trying to resolve this problem.
Thanks.
Henry
|
|
|
|
|
Make sure all your parameter values actually have values. I'd think ID, especially. (And there's no need to update the ID value, since you've used it in the WHERE. It is already the value it needs to be.)
|
|
|
|
|
Start by parameterising the WHERE condition - you clearly know how to use parameters, so why are you adding string concatenation in there and potentially causing a problem?
That's ignoring the SQL Injection vulnerability, which is a bad idea at the best of times...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi,
your T-SQL syntax is wrong. Parentheses and the VALUES keyword are used for an INSERT, not for an UPDATE. Use SET followed by a comma-separated list of fieldname=value pairs.
|
|
|
|
|
I need more caffeine...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
I think I'll go back to being an auto mechanic.
|
|
|
|
|
Nah, it is easy to read over it without noticing it. If you've been bitten by it once, you'll spot it sooner. Also fun to see how it uses parameterized queries and string-concatenation
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I am using visual studio for development.
Some code I am running run across multiple processes. For example I have a WebApi process and a consumer application. I can debug code across both process by setting up multiple startup projects.
I also wrote some unit tests.
Now the problem is, when my unit test is calling my webservices. I don't know how to run both the unit test and the web service in debug mode at the same time.
I could start the unit test without debugging and then run the unit test, but then I can't debug both across process.
How should I go about solving that development experience issue?
[EDIT]
I added a suggesion to VisualStudio feedback to make this workflow possible.
Please upvote!
Improvement to UnitTest debugging experience - Developer Community
modified 1-Apr-19 19:20pm.
|
|
|
|
|
|
I'm re-sending this as I had sent it to the Java forum by mistake.
I just found out that you can't have global variables in C#. I think they are supported in other languages like C, C++, Python and maybe Visual Basic.Net.
The program I want to convert to C# has a lot of variables that need to be used in other parts of the program such as procedures. I know you can add variables within the curly brackets for a procedure but in many cases the procedure needs to know about a lot of the variables to function and give a result and the same procedures are used many times in the code. I could use classes but I don't think you can have use the same variable in more than one class.
I'll need to experiment a bit to see if there is anyway around this.
Brian
|
|
|
|
|
Please don't do this. you have already received some suggestions, so reposting is just going to duplicate effort.
|
|
|
|
|
The idea is that you don't need global variables in C#, and by and large it's true.
You can simulate them using static :
public static class Globals
{
public static int GlobalInteger = 666;
}
...
Console.WriteLine(Globals.GlobalInteger); but I've only done it with a handful of variables in all the C# code I've ever written.
If you need lots of globals, it's normally a BIG sign that your whole design is wildly wrong.
I'd seriously think about the whole structure of your app if I were you - it's sounding very much like you didn't read any of the books after all.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
I have been reading books and took a look at some tutorials on line Griff. I used the knowledge I gained to start work in writing my programs. My first attempt was to use procedures when the code was repeating in places. I managed to get the adventure compiler to work without any problems. I'm now working on the adventure driver.
Due to the type of game it has at least 32 variables for things such as Romm Names, Room Locations, Room Descriptions etc Some procedures require many variables to produce a result and some variables are used many times in procedures. The use of arrays also helped to simplify the code.
I'm now looking at using classes as a way to wrote the adventure driver code as I can't use global variables.
Brian
|
|
|
|
|
Member 14154627 wrote: Due to the type of game it has at least 32 variables for things such as Romm Names, Room Locations, Room Descriptions etc Some procedures require many variables to produce a result and some variables are used many times in procedures. The use of arrays also helped to simplify the code.
So you have a Room class: it has a name, a location, a description, etc. Pass the room and the player and you have all the info you need!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Is this what you mean Griff
public class Room()
{
public String [] RoomName;
public int [] RoomLocation;
public String RoomDescription;
public int [,] Direction;
}
The other way would be to use a structure but classes might have been solutions.
Brian
|
|
|
|
|
No. Why would you have an array of names? Does a room have multiple names? None of the ones in my house do ...
Think about your house or flat, or whatever: what would you need to map that out as a collection of rooms?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi Griff.
Each room does have an array of directions that you can go in to enter other rooms (N,S,E,W).
Originally I used a double array suych as Direction[room number, direction] = 6
Directions were 1 - 6 for North, South,....Up, Down.
Direction[1,1] = 6. If I'm in room 1 and move North then I go to room 6.
Brian
|
|
|
|
|
Hi,
if a room (or many rooms) is a relevant item in your application domain, then by all means define a class named Room , and give it all the public properties you are going to need, such as Description , Location , Name , etc. Now start creating Room instances and use them any way you see fit.
Once you have
public class Room {
public static List<Room> AllRooms=new List<Room>();
private string name;
private Point location;
private bool isLightOn;
...
public Room(string name, Point location, ...) {
this.name=name;
AllRooms.Add(this);
}
public string Name { get {return name;}}
public Point Location { get {return location;}}
public void SwitchOnLight() {
isLightOn=true;
}
public static Room FindByName(string name) {
foreach(Room r in AllRooms) {
if (r.Name==name) return r;
}
return null;
}
}
you can operate on rooms in many ways:
Room myRoom1=new Room("kitchen", ...);
Room myRoom2=new Room("study", ...);
Room someRoom=Room.FindByName("kitchen");
someRoom.SwitchOnLight();
and you can pass an entire room, with all its properties automatically also available, to whatever method you choose to create:
Room myRoom1=new Room("kitchen", ...);
inventory(myRoom1);
If it has a name, it most certainly deserves to be described by a class!
|
|
|
|
|
Hi Luc.
Thanks for the example code.
Will this work for arrays? As I have many rooms I'm using RoomName[1] = "Hallway", RoomName[2] = "Lounge", etc.
Brian
|
|
|
|
|
Yes, you can create arrays of anything you like, as long as the items have the same type (all integers, or all rooms, or...). Most often you are better of using a List, which basically is an array that grows automatically when you add items (see AllRooms in my example).
Now is the time to stop fiddling around; do not just try and convert some existing code into C# code as long as you are unfamiliar with C# and OOP; so start reading:
1. a decent book on C#, at least the chapters that pertain to object-oriented programming;
2. a couple of CodeProject articles (including source code!) that deal with something that appeals to you.
|
|
|
|
|
Yes I admit that I have always had problems in understanding classes and now and then I come across some code that makes it a bit easier to understand.
I had considered using Lists but I think there was no direct access to the data in a list except using the foreach command.
Brian
|
|
|
|
|
You need to study the materials that are made for studying purposes; they will teach you:
1. thinking in an object-oriented way, i.e. with classes and objects
2. getting an overall grasp of a language, so you know, rather than think you know, what capabilities have been provided.
Reading a book will, in an orderly fashion, provide you with most all of the answers to the hundreds of questions you otherwise will come up with in random order; you better invest a few days learning all the concepts and possibilities, rather than wasting the same or more time in stumbling around and learning not much.
|
|
|
|
|