You must Sign In to post a response.
  • Category: ASP.NET

    Inserting the data in db using condition

    i didn't get the correct output ....there is no error .... inserting the value but dosen't checking the condition
    Any one check my coding



    MySqlDataAdapter da = new MySqlDataAdapter("select * from leavefom where empcode = '" + TextBox1.Text + "'", cn);
    DataTable dt = new DataTable();

    DataSet ds = new DataSet();
    da.Fill(ds);
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
    //Convert.ToInt32
    Double col1Value = 0;
    Double col2Value = 0;

    if (!Convert.IsDBNull(dr["Leavebal"]))
    {
    col1Value = (Double)dr["Leavebal"];
    Label3.Text = col1Value.ToString();
    }

    if (!Convert.IsDBNull(dr["Number"]))
    {

    col2Value = (Double)dr["Number"];
    Label1.Text= col2Value.ToString();
    }

    if (col1Value <= col2Value)

    {


    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert(' ...... ')", true);
    }
    else
    {
    cn.Open();
    string str3 = "insert into leavefom values('" + dplsttype.SelectedItem.Text + "','" + TextBox1.Text + "','" + Label4.Text + "','" + Label3.Text + "','" + txtfrom.Text + "', '" + dpleavefrom.SelectedItem.Text + "','" + txtto.Text + "','" + dpleaveto.SelectedItem.Text + "','" + Label1.Text + "','" + txtremark.Text + "')";
    MySqlCommand Cmd = new MySqlCommand(str3, cn);
    Cmd.ExecuteNonQuery();
    cn.Close();




    }
    }
  • #756180
    Hi,

    Better to put a break point and check line by line to resolve this kind of issues, we can't help out in this situation you should resolve it by your own, the code looks like good. Please debug the program and check whether the insert statement is trigger or not.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #756193
    Can you please let us know what is the error you are facing.

    the code seems to be ok, there may be some logic issue, I suggest please debug the code and check..

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #756208
    before executenon query you have to set a condition like if equals to some true insert process place within the if statement

  • #760934
    Hi
    ancy

    You can go through below code for your output


    SqlConnection con = new SqlConnection(strCon);
    con.Open();
    //SqlCommand sqlcmd = new SqlCommand("Insert into tabl1 values('"+ drp1.SelectedItem.Text +"'));
    //sqlcmd.ExecuteNonQuery();

    SqlDataAdapter da = new SqlDataAdapter("select * from leavefom", con);
    DataTable dt = new DataTable();
    DataSet ds = new DataSet();
    da.Fill(ds);
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
    //Convert.ToInt32
    double? col1Value = 0;
    double? col2Value = 0;

    if (!Convert.IsDBNull(dr["Leavebal"]))
    {

    col1Value = dr["Leavebal"] == DBNull.Value ? (double?)null : Convert.ToDouble(dr["Leavebal"]);
    //col1Value = (Double)dr["Leavebal"];
    string str1 = col1Value.ToString();
    }

    if (!Convert.IsDBNull(dr["Number"]))
    {
    col2Value = dr["Number"] == DBNull.Value ? (double?)null : Convert.ToDouble(dr["Number"]);
    //col2Value = (Double)dr["Number"];
    string str2 = col2Value.ToString();
    }
    if (col1Value <= col2Value)
    {
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert(' ...... ')", true);
    }
    else
    {
    //you can write your Insert Query here
    }

    }


    I have attached code tracing snapshots given below

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment


  • Sign In to post your comments