I am using asp.net 4.5 and SQL Server 2008 Express.
I want to insert a form's data into my database, connection string is OK and works in another page but in this page it seems the insert button just refreshes the page and no insert happened!
The code :
protected void Button1_Click(object sender, EventArgs e)
{
var conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
var cmdd = "insert into poroje (Pname,Pmozu,Ppayan,StartDate,EndTime,makan,tozih) values(@Pname,@Pmozu,@Ppayan,@StartDate,@EndTime,@makan,@tozih)";
using (SqlConnection cnn = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand(cmdd, cnn))
{
if (aksporoje.HasFile)
{
if (CheckFileType(aksporoje.FileName))
{
PersianCalendar Pe = new PersianCalendar();
string s = Pe.GetYear(System.DateTime.Now).ToString();
if (Directory.Exists("~/poroje/"+s))
{
Directory.CreateDirectory(Server.MapPath("~/poroje/" + s+pushePoroje(System.DateTime.Today)));
string filePath = "~/poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName;
aksporoje.SaveAs(MapPath(filePath));
cmd.Parameters.AddWithValue("@aks","poroje/"+ s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName);
}
else {
Directory.CreateDirectory(Server.MapPath("~/poroje/"+s));
Directory.CreateDirectory(Server.MapPath("~/poroje/"+s+ pushePoroje(System.DateTime.Today)));
string filePath = "~/poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName;
aksporoje.SaveAs(MapPath(filePath));
cmd.Parameters.AddWithValue("@aks", "poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName);
}
}
}
cmd.Parameters.AddWithValue("@Pname", txtnam.Text);
cmd.Parameters.AddWithValue("@Pmozu", txtmozu.Text);
cmd.Parameters.AddWithValue("@Ppayan",false);
cmd.Parameters.AddWithValue("@StartDate",PersianDateTextBox1.Text);
cmd.Parameters.AddWithValue("@EndTime",null);
cmd.Parameters.AddWithValue("@makan", txtmakan.Text);
cmd.Parameters.AddWithValue("@tozih", txttozih.Text);
try
{
cnn.Open();
int recordsAffected = cmd.ExecuteNonQuery();
}
catch (SqlException)
{
lblpeygham.Visible = true;
lblpeygham.Text = "error";
}
finally
{
cnn.Close();
lblpeygham.Visible = true;
lblpeygham.Text = "data inserted";
}
}
}
When I click on button the page refreshes amd the "data inserted" message is shown to me. But when I check the database nothing is inserted! Can you help me please?
finallypart of the code.@aksparameter you're adding? It's being added as a parameter to the command, but doesn't seem to be involved in theINSERTcommand at all.