2

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.

6
  • Show what you've got so far Commented Jun 19, 2013 at 10:34
  • What's the problem? Your code looks okay to me, is it throwing any specific error or just not inserting at all? Commented Jun 19, 2013 at 10:42
  • i have edited my question please refer to it Commented Jun 19, 2013 at 10:43
  • 1
    no error... its not inserting atall... once i insert and click add button page flickers.. i havent found any of the inserted values in DB so far Commented Jun 19, 2013 at 10:44
  • 1
    nope.. wher to call that and how .. please guide me.. Commented Jun 19, 2013 at 11:04

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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();}

        }

10 Comments

sory mate i cant get d values in back end
okie .. i didnt get ur comment (on BtnAdd_Click ou are gettin values )
im inserting values dynamically and after inserting all the values it should revert into the TABLE in DATABASE once i click ADD button..
well i went through with that article.. didnt get aby thing from that :(
This is a SQL Injection vulnerability waiting to be exploited. Do not ever concatenate user provided data into a query.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.