Rao
Rao

Reputation: 150

textbox and dropdown values inserted should store in database using c# asp.net

I got a tab container which has 4 tabs in it. In one of the tab named ADD TASK I got few fields like

(Task Name: --txtbox
Client Name:--drpdwn
Begin Date:--txtbox wid calendar
Due Date:--txtbox wid calendar
Description:--txtbox
Assign To:--drpdown
Status:--drpdown
% Complete:--drpdown)
and an ADD and CANCEL button in the end.

On running the project and inserting the values to those above mentioned fields i will click the add button and after clicking the button the values should store in my DATABASE. i have table named TASK in my DB already.

Please help me with the back end code.

here is my code

     protected void BtnAdd_Click(object sender, EventArgs e)
    {
        MTMSDTO objc = new MTMSDTO();

        int Flag = 0;

            objc.TaskName = Session["TaskName"].ToString();
            objc.ClientName = DrpClientName.SelectedItem.Text;
            objc.BeginDate = Convert.ToDateTime(TxtBeginDate.Text);
            objc.DueDate = Convert.ToDateTime(TxtDueDate.Text);
            objc.Description = Session["Description"].ToString();
            objc.AssignTo = DrpAssignTo.SelectedItem.Text;
            objc.Status = DrpStatus.SelectedItem.Text;
            objc.PercentageComplete = Convert.ToInt32(DrpPercentageComplete.Text);

            int X = obj.InsertTask(objc);
            {
                if (X >= 0)
                {
                    Flag = 1;
                }
                else
                {
                    Flag = 0;

                }
            }

            if (Flag == 1)
            {
                LblSuccess.Visible = true;
                LblSuccess.Text = "Data Added Successfully";
                Panel2.Visible = false;
            }
            else
            {
                LblErr.Visible = true;
                LblErr.Text = "Failed To Add Data!!!";
            }
        }

im using layered architecture and i have this code on my ACCESS file of DAL CLASS

      public int InsertTask(MTMSDTO M)
    {
        DBAccess db = new DBAccess();

        SqlParameter objParam = new SqlParameter("@TaskID", M.TaskID);
        objParam.Direction = ParameterDirection.Output;

        db.Parameters.Add(new SqlParameter("@TaskName", M.TaskName));
        db.Parameters.Add(new SqlParameter("@ClientName", M.ClientName));
        db.Parameters.Add(new SqlParameter("@BeginDate", M.BeginDate));
        db.Parameters.Add(new SqlParameter("@DueDate", M.DueDate));
        db.Parameters.Add(new SqlParameter("@Description", M.Description));
        db.Parameters.Add(new SqlParameter("@AssignTo", M.AssignTo));
        db.Parameters.Add(new SqlParameter("@Status", M.Status));
        db.Parameters.Add(new SqlParameter("@PercentageComplete", M.PercentageComplete));
        db.Parameters.Add(objParam);

        int retval = db.ExecuteNonQuery("InsertTask");

        if (retval >= 1)
        {
            return int.Parse(objParam.Value.ToString());
        }
        else
        {
            return -1;
        }

    }

the code is edited now but im getting error as "object reference not set to an instance of an object. " for the line (objc.TaskName = Session["TaskName"].ToString();) which is in BtnAdd_Cick.

Upvotes: 2

Views: 4239

Answers (2)

Francis Gagnon
Francis Gagnon

Reputation: 3675

Shouldn't your BtnAdd_Click function be something like this instead? You don't currently seem to be calling the InsertTask() function.

protected void BtnAdd_Click(object sender, EventArgs e) {
    MTMSDTO m = new MTMSDTO();
    m.TaskName = TxtTaskName.Text;
    m.ClientName = DrpClientName.Text;
    m.BeginDate = TxtBeginDate.Text;
    m.DueDate = TxtDueDate.Text;
    m.Description = TxtDescription.Text;
    m.AssignTo = DrpAssignTo.Text;
    m.Status = DrpStatus.Text;
    m.PercentageComplete = DrpPercentageComplete.Text;

    InsertTask(m);
}

Upvotes: 1

Maruti
Maruti

Reputation: 400

get all values in back end and pass to this function

public bool InsertRecord(string strTableName, string strColumn_Name, string strValues)
        {
 SqlConnection OBJCONNECTION;
            StringBuilder strbQuery;
            SqlCommand cmd;
            try
            {
OBJCONNECTION= new SqlConnection();
OBJCONNECTION.ConnectionString = ConfigurationManager.ConnectionStrings["Basic_ADO"].ConnectionString;//get connection string from web.config file
               OBJCONNECTION=
                strbQuery = new StringBuilder();
                strbQuery.Append("INSERT INTO ");
                strbQuery.Append(strTableName);
                strbQuery.Append("(" + strColumn_Name + ")");
                //strbQuery.Append(" VALUES");
                strbQuery.Append("(" + strValues + ")");
                cmd = new SqlCommand(strbQuery.ToString(), OBJCONNECTION);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex) { throw ex; }
            finally { strbQuery = null; cmd = null;OBJCONNECTION.close();}

        }

Upvotes: 0

Related Questions