简体   繁体   中英

Session Variable value are not storing into MS Sql Server Database

I am using two session variable and i assign the session variable to another variable and i display it get the value correctly from the session variable but when i try to insert into MS Sql Server Database the value is not being insert into the database...any idea?

Below is my Code:

Session["selected"] = "apple";
Session["current"] = 1;

string mycategory = Session["selected"].ToString();
int myId = Convert.ToInt32(Session["current"]);

con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO [Marking] VALUES (@photoid, @photocategoryjudge, @totalmarks)", con);
cmd.Parameters.Add("@photoid", SqlDbType.Int).Value = myId;
cmd.Parameters.Add("@photocategoryjudge", SqlDbType.VarChar).Value = mycategory;
cmd.Parameters.Add("@totalmarks", SqlDbType.Int).Value = totalMarks;
cmd.ExecuteNonQuery();
con.Close();

I don't have the answer, but I have some simple steps for you that will help you debug, and the basis of which are good coding standards and can be used in some variation for future projects,

The first thing to do is to implement exception handling, and this would be done via a try...catch...finally block. You will Try the block of code for the majority of the actual SQL command, and if there is an error is thrown it will throw you into the Catch block. Assigning a variable to the Exception will allow you to hover to review the details. The catch can be stacked for different conditions, with Exception being the last in it's kind which will catch all types of exceptions. This is kind of like a case statement in that only one of the catch series will fire. The last portion of this block is the Finally statement, which will run after the intended commands finish or the exception handling occurs.

What you are not utilizing is that the ExecuteNonQuery() method actually has a return value (int32) which represents the Rows Affected value of the Sql Statement. For a simple insert statement this should be 1. If this was an update command this could be 0 or higher, depending on how many rows there are or any conditions. Either way it will always be equal to or greater than 0. How I have implemented here it will assign a negative value if there is an error in the coding, and a different value for each type of exception.

Last thing is to run your code in debug mode with breakpoints. When you hit the breakpoint see which of your variables have what values. This will be helpful to know what error is occurring if any. You can check your Sql statement and try running directly in SSMS if that is the error source.

good luck

Session["selected"] = "apple";
Session["current"] = 1;

string mycategory = Session["selected"].ToString();
int myId = Convert.ToInt32(Session["current"]);

int ra; //                                 ra = Rows Affected

try {
    con.Open();
    SqlCommand cmd = new SqlCommand("INSERT INTO [Marking] VALUES (@photoid, @photocategoryjudge, @totalmarks)", con);
    cmd.Parameters.Add("@photoid", SqlDbType.Int).Value = myId;
    cmd.Parameters.Add("@photocategoryjudge", SqlDbType.VarChar).Value = mycategory;
    cmd.Parameters.Add("@totalmarks", SqlDbType.Int).Value = totalMarks;
    ra = cmd.ExecuteNonQuery();
}
catch (SqlException sx) {
    ra = -2; //                             breakpoint here
    // If you stop here, your SQL has an error. Hover on sx for detail
    // Error handling routine
catch (Exception ex) {
    ra = -1; //                             breakpoint here
    // non-sql error block. Hover on ex for more info
    // Error handling routine
}
finally {

    con.Close();
}

int Results = ra; //                        breakpoint here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM