Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
code as follows;

private SqlDataCon SCon = new SqlDataCon();
private SqlDataReader Dr;

insert code as follows;
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        Label6.Text = "";

        if (FromDate.SelectedDateValue.ToString() == "")
        {
            Label6.Text = "From date is not selected";
            return;
        }
        if (Todate.SelectedDateValue.ToString() == "")
        {
            Label6.Text = "To date is not selected";
            return;
        }

            Sql = "insert into BirthDayWish values('" + txt_name.Text + "','" + FromDate.SelectedDateValue.ToString() + "','" + txt_mobile.Text + "','" + Todate.SelectedDateValue.ToString() + "','a','" + txt_Email.Text + "')";
            try
            {
            Dr = SCon.ReadSql(Sql);
            //GridView1.DataSource = Dr;
            //GridView1.DataBind();
            Dr.Close();
        }

        catch (Exception Ex1)
        {
            Response.Write(Ex1);
        }
    }


but is not saving in the database why>

from my code please correct
Posted
Comments
Surendra0x2 25-Dec-12 6:57am    
use sqlcommand.executenonquery(); for DML command like insert update delete

May be sql database date field is DateTime,and your insert date formate is not fallow right formate.
 
Share this answer
 
Hi first of all your code is not safe =)
You have opened it for SQL injections, so i strongly asking you to change sql statement to use sql parameters!!
That was one sugestions.
Another one, as far sa i understand, you don't even try to execute insert operation to DB, since SCon.ReadSql(Sql) -> performs read operation!!!

So change your code to:
C#
 try
        {
            con.Open();
            Sql = "insert into BirthDayWish values(@p1,@p2,@p3,@p4,#p5)";
            SqlCommand cmd = new SqlCommand(Sql, con);
            cmd.Parameters.Add("@p1", SqlDbType.VarChar);
            cmd.Parameters["@p1"].Value = txt_name.Text;
            DateTime dt;
            if(!DateTime.TryParse(FromDate.SelectedDateValue.ToString(),out dt))
            {// error in date format!!!}  
            cmd.Parameters.Add("@p2", SqlDbType.DateTime);
            cmd.Parameters["@p2"].Value = dt;           
/* another stuff goes here!!!*/
 
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception Ex1)
        {
            Response.Write(Ex1);
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900