locked
Single Record Update in table RRS feed

  • Question

  • User-749431702 posted

    Hi everyone. i want to update a record in table using dataset but it is not updating. plz tell me how to do that. i'm feteching details in textboxs and on click of button sending data of text boxes to database table. i'm using Dataset and SqlDataAdapter  Also plz tell me how to delete a record

    Plz help me. its urgent for me.

    Any response is appriciated. Thanks in advance

    Thursday, May 19, 2011 4:53 AM

Answers

  • User3866881 posted

    Hello:)

    You should try to say this:

    DataRow[] customerRow =
    ds.Tables[0].Select("sl_no = '" + txtSerialNumber.Text + "'");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 21, 2011 9:32 PM

All replies

  • User-386954757 posted

    Please give some more info

    Thursday, May 19, 2011 5:15 AM
  • User1867929564 posted

    is it neccessary to use dataset ?
    why don't you directly update/delete using OledbCommand ?

    refer this,

    http://www.c-sharpcorner.com/UploadFile/Ashish1/dataset02052006180740PM/dataset.aspx 

    Thursday, May 19, 2011 6:45 AM
  • User-109091561 posted

    use this one method inside class & pass your query from form to class

    public int Update(string query)

    {

    con = new SqlConnection(strConn);

    cmd = new SqlCommand(query, con);

    con.Open();

    i = cmd.ExecuteNonQuery();

    con.Close();

    return i;

    }

     

    Thursday, May 19, 2011 7:39 AM
  • User-109091561 posted

    Code for deleting a Record

    public int Delete(string query)

    {

    con = new SqlConnection(strConn);

    cmd = new SqlCommand(query, con);

    con.Open();

    i = cmd.ExecuteNonQuery();

    con.Close();

    return i;

    }

     

    Mark this as answer if it helps u

     

    Thursday, May 19, 2011 7:42 AM
  • User-749431702 posted

    i'm using this code. plz tell me what is missing.

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Drawing;
    public partial class UpdateVender : System.Web.UI.Page
    {
        string str = ConfigurationManager.ConnectionStrings["myserver"].ConnectionString;
        SqlConnection con;
        SqlDataAdapter adp1;
        SqlCommandBuilder build;
        DataSet ds1;
        SqlCommand cmd, cmd1;
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void btnSubmitForm_Click(object sender, EventArgs e)
        {
            string qry = @"select * from vendor_details";
    
            SqlConnection conn = new SqlConnection(str);
    
            try
            {
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(qry, conn);
    
                SqlCommandBuilder cb = new SqlCommandBuilder(da);
    
                DataSet ds = new DataSet();
                da.Fill(ds, "vendor_details");
    
                DataTable dt = ds.Tables["vendor_details"];
    
    
    
                DataRow[] customerRow =
         ds.Tables["Vendor_details"].Select("sl_no = '" + txtSerialNumber.Text + "'");
    
                customerRow[0]["vendor_name"] = txtVenderName.Text;
                customerRow[0]["contact_person"] = txtConatctPerson.Text;//
                customerRow[0]["address"] = txtAddress.Text;
                customerRow[0]["city"] = txtCity.Text;
                customerRow[0]["state"] = txtState.Text;
                customerRow[0]["zip_code"] = txtZipCode.Text;
                customerRow[0]["country"] = txtCountry.Text;
                customerRow[0]["email_id"] = txtEmailID.Text;
                customerRow[0]["contact_no"] = txtContactNumber.Text;
            }
            finally
            {
                conn.Close();
            }
    
        }
    
        protected void btnResetForm_Click(object sender, EventArgs e)
        {
            clear();
        }
        protected void btnsearch_Click(object sender, EventArgs e)
        {
            con = new SqlConnection(str);
            con.Open();
            adp1 = new SqlDataAdapter("select * from vendor_details where sl_no='" + txtSerialNumber.Text + "'", con);
            build = new SqlCommandBuilder(adp1);
            ds1 = new DataSet();
            adp1.Fill(ds1, "vendor_details");
            DataTable dt1 = ds1.Tables["vendor_details"];
            DataRow dr1 = dt1.NewRow();
    
            if (ds1.Tables[0].Rows.Count > 0)
            {
                txtVenderName.Text = ds1.Tables[0].Rows[0]["vendor_name"].ToString();
                txtConatctPerson.Text = ds1.Tables[0].Rows[0]["contact_person"].ToString();
                txtAddress.Text = ds1.Tables[0].Rows[0]["address"].ToString();
                txtCity.Text = ds1.Tables[0].Rows[0]["city"].ToString();
                txtState.Text = ds1.Tables[0].Rows[0]["state"].ToString();
                txtZipCode.Text = ds1.Tables[0].Rows[0]["zip_code"].ToString();
                txtCountry.Text = ds1.Tables[0].Rows[0]["country"].ToString();
                txtEmailID.Text = ds1.Tables[0].Rows[0]["email_id"].ToString();
                txtContactNumber.Text = ds1.Tables[0].Rows[0]["contact_no"].ToString();
            }
            con.Close();
    
        }
        void clear()
        {
            txtVenderName.Text = string.Empty;
            txtConatctPerson.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtCity.Text = string.Empty;
            txtState.Text = string.Empty;
            txtZipCode.Text = string.Empty;
            txtCountry.Text = string.Empty;
            txtEmailID.Text = string.Empty;
            txtContactNumber.Text = string.Empty;
        }
    }
    

    Also tell me abt delete.

    Friday, May 20, 2011 8:54 AM
  • User3866881 posted

    public partial class UpdateVender : System.Web.UI.Page

        static string str = ConfigurationManager.ConnectionStrings["myserver"].ConnectionString;
        static SqlDataAdapter adapter = new SqlDataAdapter("select * from vendor_details", new SqlConnection(Your conn str));
     static DataSet ds = new DataSet();
     static UpdateVender()
     {
      SqlCommandBuilder sbu = new SqlCommandBuilder(adapter);
      adapter.Fill(ds);
     }
     protected void Page_Load(object sender, EventArgs e)
        {
     
        }
     
        protected void btnSubmitForm_Click(object sender, EventArgs e)
        {

                DataRow[] customerRow =
         ds.Tables["Vendor_details"].Select("sl_no = '" + txtSerialNumber.Text + "'");
     
                customerRow[0]["vendor_name"] = txtVenderName.Text;
                customerRow[0]["contact_person"] = txtConatctPerson.Text;//
                customerRow[0]["address"] = txtAddress.Text;
                customerRow[0]["city"] = txtCity.Text;
                customerRow[0]["state"] = txtState.Text;
                customerRow[0]["zip_code"] = txtZipCode.Text;
                customerRow[0]["country"] = txtCountry.Text;
                customerRow[0]["email_id"] = txtEmailID.Text;
                customerRow[0]["contact_no"] = txtContactNumber.Text;
       adapter.Update(ds);
            }
          
     
        }
     
        protected void btnResetForm_Click(object sender, EventArgs e)
        {
            clear();
        }
        protected void btnsearch_Click(object sender, EventArgs e)
        {
            con = new SqlConnection(str);
            con.Open();
            adp1 = new SqlDataAdapter("select * from vendor_details where sl_no='" + txtSerialNumber.Text + "'", con);
            build = new SqlCommandBuilder(adp1);
            ds1 = new DataSet();
            adp1.Fill(ds1, "vendor_details");
            DataTable dt1 = ds1.Tables["vendor_details"];
            DataRow dr1 = dt1.NewRow();
     
            if (ds1.Tables[0].Rows.Count > 0)
            {
                txtVenderName.Text = ds1.Tables[0].Rows[0]["vendor_name"].ToString();
                txtConatctPerson.Text = ds1.Tables[0].Rows[0]["contact_person"].ToString();
                txtAddress.Text = ds1.Tables[0].Rows[0]["address"].ToString();
                txtCity.Text = ds1.Tables[0].Rows[0]["city"].ToString();
                txtState.Text = ds1.Tables[0].Rows[0]["state"].ToString();
                txtZipCode.Text = ds1.Tables[0].Rows[0]["zip_code"].ToString();
                txtCountry.Text = ds1.Tables[0].Rows[0]["country"].ToString();
                txtEmailID.Text = ds1.Tables[0].Rows[0]["email_id"].ToString();
                txtContactNumber.Text = ds1.Tables[0].Rows[0]["contact_no"].ToString();
            }
            con.Close();
     
        }
        void clear()
        {
            txtVenderName.Text = string.Empty;
            txtConatctPerson.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtCity.Text = string.Empty;
            txtState.Text = string.Empty;
            txtZipCode.Text = string.Empty;
            txtCountry.Text = string.Empty;
            txtEmailID.Text = string.Empty;
            txtContactNumber.Text = string.Empty;
        }
    }

    Saturday, May 21, 2011 4:28 AM
  • User-749431702 posted

    but this code is giving me this error. i'm pasting the error

     

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    Source Error: 

    Line 256:    {Line 257:Line 258:        DataRow[] customerRow = ds.Tables["Vendor_details"].Select("sl_no = '" + txtSerialNumber.Text + "'");Line 259:Line 260:        customerRow[0]["vendor_name"] = txtVenderName.Text;
    Source File: f:\New Folder\zen\vender\UpdateVender.aspx.cs    Line: 258 
    Stack Trace: 

    [NullReferenceException: Object reference not set to an instance of an object.]   UpdateVender.btnSubmitForm_Click(Object sender, EventArgs e) in f:\New Folder\zen\vender\UpdateVender.aspx.cs:258   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

    Saturday, May 21, 2011 11:07 AM
  • User3866881 posted

    Hello:)

    You should try to say this:

    DataRow[] customerRow =
    ds.Tables[0].Select("sl_no = '" + txtSerialNumber.Text + "'");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 21, 2011 9:32 PM
  • User-749431702 posted

    Thanks you so much. it solved.  

    Sunday, May 22, 2011 1:13 AM