Reputation: 11
I've created a website that is connected to the database, I can get data and display it on the page with no prob at all. But when I try to insert (or update) it does nothing.
I have tested the SQL query and it works just fine.
I've looked here for similar situations and questions here for the past 24 hours with no luck.
I want the website to tell if the user want a one way or two way tickets and make a request. The table has the request id which is automatically incremented then I add the id of the student who is requesting, the the id of the departure ticket then the id of return ticket (if it is 2 ways, this value can be null) there is also status which will be pending until a supervisor either accept or decline the request, once accepted, issue date will be added and status will change to approved. If declined reason will be added and status change to declined.
Main issue, when I make the request, the row is not created and added to the database for the supervisor to view later.
Here is my code:
protected void Button1_Click(object sender, EventArgs e)
{
int parsedValue;
int.TryParse(DropDownList1.SelectedValue, out parsedValue);
SqlConnection myConnection = new SqlConnection(""); // I removed the connection string.
string sqlcommand = "";
string idString = TextBox1.Text;
string idTwoString ="";
bool canContune = false;
if (parsedValue == 1)
{
System.Diagnostics.Debug.WriteLine("p");
Panel3.Visible = true;
idTwoString = TextBox2.Text;
if (AllNumber(idString, TextBox1) && AllNumber(idTwoString, TextBox2))
{
canContune = true;
}
}
else if (AllNumber(idString, TextBox1))
{
canContune = true;
}
if (canContune)
{
int dId;
int dId2;
int.TryParse(idString, out dId);
int.TryParse(idTwoString, out dId2);
sqlcommand = "INSERT INTO TicketRequest.dbo.TicketRequest (student_id, departure_id, return_id, statues, issue_date, notes) "
+ "VALUES (@student_id, @departure_id , @return_id , @statues, @issue_date, @notes)";
try
{
SqlCommand cmd = new SqlCommand(sqlcommand);
cmd.CommandType = CommandType.Text;
cmd.Connection = myConnection;
myConnection.Open();
cmd.Parameters.Add("@student_id", SqlDbType.Int).Value = id;
cmd.Parameters.Add("departure_id", SqlDbType.Int).Value = dId; //I used AddWithValue(@para, value) it didn't work.
if (parsedValue == 0)
{
cmd.Parameters.AddWithValue("@return_id", DBNull.Value);
}
else
{
cmd.Parameters.Add("@return_id", SqlDbType.Int).Value = dId2;
}
cmd.Parameters.Add("@statues", SqlDbType.Text).Value = "Pending";
cmd.Parameters.AddWithValue("@issue_date", DBNull.Value);
cmd.Parameters.AddWithValue("@notes", DBNull.Value);
cmd.ExecuteNonQuery();
myConnection.Close();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
}`
It doesn't throw any exception, I really don't know what is wrong. I would be very thankful to anyone who will point me out my mistake in Insert query. Thanks in advance.
==================================================
I apologized all, it worked just fine. it seemed that the code wasn't excuted to being with. Thanks Falanor, you helped me discover the problem. =)
Upvotes: 0
Views: 202
Reputation: 206
Try to check the return value.
int modified =(int)cmd.ExecuteScalar();
This is also missing the @ symbol for the parameter
cmd.Parameters.Add("departure_id", SqlDbType.Int).Value = dId; //I used AddWithValue(@para, value) it didn't work.
Upvotes: 4