Click here to Skip to main content
15,888,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello frndz , I m trying to update the data from my asp.net form
I m having gridview ,wwhen i clik on particular row in grid view it displays all the data into
respective texboxes. I want to update this data .I tried to write a query but it showed me an error.
Here i my full code of c#

public partial class Company_Master : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lbluser.Text = Session["user_id"].ToString();
            lbldiv.Text = Session["division_id"].ToString();
            lblunit.Text = Session["unitcode"].ToString();
            lblloyr.Text = Session["year"].ToString();
            lbldate.Text = Session["date"].ToString();
        }

        Util.JQueryUtils.RegisterTextBoxForDatePicker(Page, txtregdt);
        DataTable dt;
        OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
        OleDbCommand cmd1 = new OleDbCommand("SELECT cmp_id,cmp_name,reg_no,reg_dt,addr_line_1,addr_line_2,addr_line_3,pin_code,lst_no,cst_no,fax,phone,emailid FROM company_master ORDER BY cmp_id ASC", conn);
        OleDbDataAdapter dA = new OleDbDataAdapter(cmd1);
        dt = new DataTable();
        dA.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Session["comp"] = dt;
        GridView1.Visible = true;
        
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
            string query = "INSERT INTO company_master(cmp_id,cmp_name,reg_no,reg_dt,addr_line_1,addr_line_2,addr_line_3,pin_code,lst_no,cst_no,fax,phone,emailid) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
            using (myConnection)
            {
                myConnection.Open();
                using (OleDbCommand cmd = new OleDbCommand(query, myConnection))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("cmp_id", txtcompid.Text);
                    cmd.Parameters.AddWithValue("cmp_name", txtcompname.Text);
                    cmd.Parameters.AddWithValue("reg_no", txtregno.Text);
                    cmd.Parameters.AddWithValue("reg_dt", DateFormat.SplitDateStringAndReturnObject(txtregdt.Text));
                    cmd.Parameters.AddWithValue("addr_line_1", txtaddr.Text);
                    cmd.Parameters.AddWithValue("addr_line_2", txtaddr1.Text);
                    cmd.Parameters.AddWithValue("addr_line_3", txtaddr2.Text);
                    cmd.Parameters.AddWithValue("pin_code" ,txtpin.Text);
                    cmd.Parameters.AddWithValue("lst_no",txtlocalsale.Text);
                    cmd.Parameters.AddWithValue("cst_no",txtcentraltxt.Text);
                    cmd.Parameters.AddWithValue("fax", txtfax.Text);
                    cmd.Parameters.AddWithValue("phone", txtphone.Text);
                    cmd.Parameters.AddWithValue("emailid", txtemail.Text);

                    cmd.ExecuteNonQuery();
                }
                myConnection.Close();
            }

        }
        catch (Exception ex)
        {
            Response.Write("Error In An Application :" + ex.StackTrace + ex.Message);
        }
    }
    
    private class DateFormat
    {
        public static DateTime SplitDateStringAndReturnObject(string DateString)
        {
            if (DateString == "" || DateString == null)
            {

                int year = 9999;
                int month = 1;
                int day = 1;
                DateTime dtObj = new DateTime(year, month, day);
                return dtObj;
            }
            else
            {
                int len;
                string[] datesplit = DateString.Split('-');
                len = datesplit.Length;
                if (len < 3)
                {
                    datesplit = DateString.Split('/');
                }

                int year = Convert.ToInt32(datesplit[2]);
                int month = Convert.ToInt32(datesplit[1]);
                int day = Convert.ToInt32(datesplit[0]);
                DateTime dtObj = new DateTime(year, month, day);
                return dtObj;
            }

        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtcompid.Text = GridView1.SelectedDataKey.Values[0].ToString();
        txtcompname.Text = GridView1.SelectedDataKey.Values[1].ToString();
        txtregno.Text = GridView1.SelectedDataKey.Values[2].ToString();
        txtregdt.Text = GridView1.SelectedDataKey.Values[3].ToString();
        txtaddr.Text = GridView1.SelectedDataKey.Values[4].ToString();
        txtaddr1.Text = GridView1.SelectedDataKey.Values[5].ToString();
        txtaddr2.Text = GridView1.SelectedDataKey.Values[6].ToString();
        txtpin.Text = GridView1.SelectedDataKey.Values[7].ToString();
        txtlocalsale.Text = GridView1.SelectedDataKey.Values[8].ToString();
        txtcentraltxt.Text = GridView1.SelectedDataKey.Values[9].ToString();
        txtfax.Text = GridView1.SelectedDataKey.Values[10].ToString();
        txtphone.Text = GridView1.SelectedDataKey.Values[11].ToString();
        txtemail.Text = GridView1.SelectedDataKey.Values[12].ToString();
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
            OleDbCommand cmd = new OleDbCommand("UPDATE company_master SET cmp_name = '" + txtcompname.Text + "',reg_no = '" + txtregno.Text + "',reg_dt ='" + 

DateFormat.SplitDateStringAndReturnObject(txtregdt.Text) + "',addr_line_1 = '" + txtaddr.Text 

+ "',addr_line_2 = '" + txtaddr1.Text + "',addr_line_3 = '" + txtaddr2.Text + "',pin_code = '" 

+ txtpin.Text + "',lst_no = '" + txtlocalsale.Text + "',cst_no ='" + txtcentraltxt.Text + 

"',phone = '" + txtphone.Text + "',email ='" + txtemail.Text + "' WHERE cmp_id ='" + 

txtcompid.Text + "'", con);
            con.Open();
            cmd.CommandType = CommandType.Text;
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            cmd.CommandType = CommandType.Text; 
            cmd.ExecuteNonQuery();
            Response.Write("Update Success");
            con.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}

Please help me to write the query

after updation gridvew should also be updated
Posted
Updated 19-Apr-13 10:23am
v2
Comments
Richard C Bishop 19-Apr-13 16:24pm    
What error and which line?
Devendra Dighe 19-Apr-13 16:32pm    
System.Data.OleDb.OleDbException: No value given for one or more required parameters.

line 139 it showed error in executenonquery
Richard C Bishop 19-Apr-13 16:37pm    
I would recommend using parameterized queries for the UPDATE statement as you did for the Insert statement. Debug the application and figure out which value is empty before you execute the query.
Devendra Dighe 20-Apr-13 1:55am    
I ussed parametrized query but the query returns 0 it does not update the data

when i select row from gridview it shows both date & time in textbox but in database only date is stored
when i debugged code the date field showed 0 value
now how to solve this problem???
Richard C Bishop 23-Apr-13 9:45am    
If you convert the value to a DateTime before you enter it into the database, the time should be there as well.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900