Ok, so I'm totally stuck, I really didn't want to have to ask for help but I'm at the breaking point. I am attempting to create a web game; I am trying to add a class to instantiate fish objects, and populate their information into a SQL database. I have verified the SQL connection string because it is being used successfully in another portion of the code. It seems to go to the catch block for an exception at the line "cmdIns.ExecuteNonQuery();". If you can help me to understand what I might be doing wrong that would be great. If you can provide a better way to achieve the same results, that would be welcome as well.
Thank you,
Chris Witte
namespace WebApplication1
{
public class AddFish
{
public void Main(int iterations,Page pg)
{
//Sets Query string
string sqlIns = "INSERT INTO Fish (Species_ID, Size, LocationX, LocationY) VALUES (@Species_ID, @Size, @LocationX, @LocationY)";
//Create connection
SqlConnection db = new SqlConnection();
//Retrieve connectionstring from method, that calls into system.web
//verrified works
GetConnectionString1 connstr = new GetConnectionString1();
db.ConnectionString= (connstr.GetConnectionString(null));
//Makes a new instance of SqlCommand
SqlCommand cmdIns = new SqlCommand(sqlIns,db);
try
{
//Generate needed random numbers
Random x = new Random(Environment.TickCount + 1);
Random y = new Random(Environment.TickCount + 2);
Random z = new Random(Environment.TickCount + 3);
int swt = x.Next(0, 1);
int size = 0;
int locy = 0;
//Switch using random number
switch (swt.ToString())
{
//For case 0 create a Bass and add it to DB
case "0":
Bass myBass = new Bass();
// Generate random number for size
size = y.Next(4, 32);
// Assigns values
myBass.Size = Convert.ToInt32(size);
myBass.LocationX = 1;
locy = z.Next(1, 2);
myBass.LocationY = Convert.ToInt32(locy);
// ADD Values to the sqlcommand
cmdIns.Parameters.AddWithValue("@Species_ID", 1);
cmdIns.Parameters.AddWithValue("@Size", myBass.Size);
cmdIns.Parameters.AddWithValue("@LocationX", myBass.LocationX);
cmdIns.Parameters.AddWithValue("@LocationY", myBass.LocationY);
// Open connection
db.Open();
// Executes command
cmdIns.ExecuteNonQuery();
// Clears the sqlcommand info
cmdIns.Parameters.Clear();
break;
case "1":
Catfish myCat = new Catfish();
size = y.Next(8, 48);
myCat.Size = Convert.ToInt32(size);
myCat.LocationX = 1;
locy = z.Next(1,2);
myCat.LocationY = Convert.ToInt32(locy);
cmdIns.Parameters.AddWithValue("@Species_ID", 1);
cmdIns.Parameters.AddWithValue("@Size", myCat.Size);
cmdIns.Parameters.AddWithValue("@LocationX", myCat.LocationX);
cmdIns.Parameters.AddWithValue("@LocationY", myCat.LocationY);
db.Open();
cmdIns.ExecuteNonQuery();
cmdIns.Parameters.Clear();
break;
}
}
catch (Exception)
{
MessageBox msg = new MessageBox("Error While Sending Data --- ADDFISH", pg);
}
finally
{
db.Close();
}
}
}
}

New Topic/Question
Reply




MultiQuote





|