Click here to Skip to main content
11,502,383 members (62,206 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
code as follows;

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

insert code as follows;
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 25-Dec-12 0:34am
Comments
Surendra0x2 at 25-Dec-12 6:57am
   
use sqlcommand.executenonquery(); for DML command like insert update delete
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 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);
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

May be sql database date field is DateTime,and your insert date formate is not fallow right formate.
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 25 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100