[Solved] Unable to save (insert) TextBox value to database using C# in ASP.Net

indradeo
 
on Mar 17, 2020 01:58 AM
974 Views

unable to save Total_one_four, Total_five_six, Net_claim, Claim_dDate values using asp c#

namespace TourPro
{
    public partial class one : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string str;
        SqlCommand com;
        protected void Page_Load(object sender, EventArgs e)
        {
            {
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                str = "select * from two";
                com = new SqlCommand(str, con);
                SqlDataReader reader = com.ExecuteReader();
                reader.Read();
                TextBox1.Text = reader["Amt"].ToString();
            }

            {    
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                str = "select * from two";
                com = new SqlCommand(str, con);
                SqlDataReader reader = com.ExecuteReader();
                reader.Read();
                TextBox2.Text = reader["Amt"].ToString();
            }

            {
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                str = "select * from Sec_four";
                com = new SqlCommand(str, con);
                SqlDataReader reader = com.ExecuteReader();
                reader.Read();
                TextBox3.Text = reader["amount_hotel"].ToString();
            }
            
            {
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                str = "select * from Sec_four";
                com = new SqlCommand(str, con);
                SqlDataReader reader = com.ExecuteReader();
                reader.Read();
                TextBox6.Text = reader["amount_hotel"].ToString();
            }
            {
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                str = "select * from Sec_four";
                com = new SqlCommand(str, con);
                SqlDataReader reader = com.ExecuteReader();
                reader.Read();
                TextBox4.Text = reader["amount_hotel"].ToString();
            }
            {
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                str = "select * from Sec_four";
                com = new SqlCommand(str, con);
                SqlDataReader reader = com.ExecuteReader();
                reader.Read();
                TextBox7.Text = reader["amount_daily_allowance"].ToString();
            }
        }
       
        protected void Button3_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO one VALUES(@Journey_fares,@Conve_carges,@Accomm_charges,@Dally_allowance,@Total_one_four,@Corporate_centre,@From_project,@Total_five_six,@Net_claim,@Claim_dDate)", con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@Journey_fares", TextBox1.Text.Trim());
                    cmd.Parameters.AddWithValue("@Conve_carges", TextBox2.Text.Trim());
                    cmd.Parameters.AddWithValue("@Accomm_charges", TextBox3.Text.Trim());
                    cmd.Parameters.AddWithValue("@Dally_allowance", TextBox4.Text.Trim());
                    cmd.Parameters.AddWithValue("@Total_one_four", TextBox5.Text.Trim());
                    cmd.Parameters.AddWithValue("@Corporate_centre", TextBox6.Text.Trim());
                    cmd.Parameters.AddWithValue("@From_project", TextBox7.Text.Trim());
                    cmd.Parameters.AddWithValue("@Total_five_six", TextBox8.Text.Trim());
                    cmd.Parameters.AddWithValue("@Net_claim", TextBox9.Text.Trim());
                    cmd.Parameters.AddWithValue("@Claim_dDate", TextBox10.Text.Trim());
            
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    
                    TextBox1.Text = string.Empty;
                    TextBox2.Text = string.Empty;
                    TextBox3.Text = string.Empty;
                    TextBox4.Text = string.Empty;
                    TextBox5.Text = string.Empty;
                    TextBox6.Text = string.Empty;
                    TextBox7.Text = string.Empty;
                    TextBox8.Text = string.Empty;
                    TextBox9.Text = string.Empty;
                }
            }
        }
Download FREE API for Word, Excel and PDF in ASP.Net: Download
arunkurmi
 
on Mar 17, 2020 07:49 AM

Hi indradeo,

You did not set the TextBox values for TextBox5, TextBox8, TextBox9, TextBox10 and assigned the value in the parameter. So the value are inserted as null in the table.

Please assign or type before save the record in database table else it will be saved as null.