My program takes user input and uses it to create a query. The results of that query will then be put into an XML file with XElements based on their selections. I have a string staff
which i set equal to the value of staffType.
Where staff is declared w/ snippet of query code:
using (
var conn = new SqlConnection("Server=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Database=KUDERDEV;User ID=xxxxxxxxxxxxxxxxxx;Password= xxxxxxxx;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
)
{
conn.Open();
bool quit = false;
string choice;
string staff;
SqlCommand cmd = new SqlCommand();
while (!quit)
{
Console.WriteLine("Sort by staffType: K12Staff, psStaff, WFStaff or none?");
string staffType = Console.ReadLine();
staff = staffType;
if (staffType == "K12Staff")
{
Console.WriteLine("Sort by code, date, both, or none?");
choice = Console.ReadLine();
switch (choice)
{
case "code":
Console.WriteLine("Sort by code1 or code2?");
string codeCol = Console.ReadLine();
Console.WriteLine("Enter desired code");
string code = Console.ReadLine();
cmd = new SqlCommand("SELECT * FROM Staff WHERE (Staff." + @codeCol + "='" + @code + "') AND staffType ='" + @staffType + "' FOR XML PATH('staff'), ROOT('k12Staff')", conn);
quit = true;
staff = staffType;
break;
When the query string is complete, I go into another using statement without closing the first one to write the XML file. Here I want to change the format of the XML (XElement) depending on which staffType was chosen.
Writing the XML file snippet:
using (cmd)
{
using (var reader = cmd.ExecuteXmlReader())
{
var doc = XDocument.Load(reader);
string path = @"Staff." + DateTime.Now.ToString("yyyyMMdd") + ".xml";
using (var writer = new StreamWriter(path))
{
//if (staff == "k12Staff")
XNamespace ns = "http://specification.sifassociation.org/Implementation/na/3.2/html/CEDS/K12/K12_k12Staff.html";
var root = new XElement(ns + "k12Staff");
foreach (var d in doc.Descendants("staff"))
{
root.Add(new XElement(ns + "staff",
new XElement(ns + "identity",
new XElement(ns + "name",
new XElement(ns + "firstName", first),
new XElement(ns + "lastName", last)
)
),
new XElement(ns + "employment",
new XElement(ns + "positionTitle", position)
),
new XElement(ns + "assignment",
new XElement(ns + "leaID", leaID),
new XElement(ns + "schoolID", schoolID)
),
new XElement(ns + "contact",
new XElement(ns + "phoneNumberList",
new XElement(ns + "number", phone),
new XElement(ns + "phoneNumberIndicator", indicator)
),
new XElement(ns + "emailList",
new XElement(ns + "email", email)
)
),
new XElement(ns + "delete", delete)
Then if the staffType was something different such as "psStaff" then I would change the format of the XML (XElement) to have different names, positions, etc.
So it would be like:
if(staff == "k12Staff"){
format xml here...
}
else if (staff == "psStaff"){
format xml here....
}
etc etc..
My Problem:
In the previous example, my code has if(staff == "k12Staff")
but I am told that it is an assigned local variable. I have tried declaring staff outside of the using statements as well as trying to use staff
in a using statement like so using(staff)
which is how I used my cmd
variable. How come my program recognizes cmd
but not staff
?
You did not post the entire code, so it is not possble to say, where your variable staff
should get a value and why it doesn't.
Not assigned means: existing but never got a value...
Try the following: At the place, where you declare your variable change this:
string staff="defaultValue";
On the place where you handle the variable's content change this:
if(staff == "k12Staff"){
format xml here...
}
else if (staff == "psStaff"){
format xml here....
}
else if (staff == "defaultValue"){
//What ever is to be done if all attempts to set "staff" did not work
//You must set a stop mark before your `while (!quit)` and step through.
//There is at least one situation, where `staff` is not set to any value...
//If there's a bug you must fix this, if this is allowed to happen, solve it here...
}