Click here to Skip to main content
15,917,062 members
Home / Discussions / C#
   

C#

 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell13-May-15 19:53
Norris Chappell13-May-15 19:53 
GeneralRe: Update in C# using SQL Server Database. Pin
Agent__00713-May-15 20:05
professionalAgent__00713-May-15 20:05 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell13-May-15 20:11
Norris Chappell13-May-15 20:11 
GeneralRe: Update in C# using SQL Server Database. Pin
Agent__00713-May-15 20:20
professionalAgent__00713-May-15 20:20 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell13-May-15 20:24
Norris Chappell13-May-15 20:24 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell13-May-15 20:30
Norris Chappell13-May-15 20:30 
GeneralRe: Update in C# using SQL Server Database. Pin
Agent__00713-May-15 20:43
professionalAgent__00713-May-15 20:43 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell13-May-15 20:50
Norris Chappell13-May-15 20:50 
Hi, I have finally got the edit, update and delete to work. However, for the update can you have more than 1 set statement with the update command? I want to thanks everyone for there help and comments.

C#
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls.WebParts;

namespace StaffingWebParts.keypernew
{
    public partial class keypernewUserControl : UserControl
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                KeyPersonnel();
            }
        }
        protected void KeyPersonnel()
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("Select id, Name from CMS_Key_Personnel", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            int count = ds.Tables[0].Rows.Count;
            conn.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvKeyPersonnel.DataSource = ds;
                gvKeyPersonnel.DataBind();
            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                gvKeyPersonnel.DataSource = ds;
                gvKeyPersonnel.DataBind();
                int columncount = gvKeyPersonnel.Rows[0].Cells.Count;
                lblmsg.Text = " No data found !!!";
            }
        }
        protected void gvKeyPersonnel_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvKeyPersonnel.EditIndex = e.NewEditIndex;
            KeyPersonnel();
        }
        protected void gvKeyPersonnel_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            

           // string id = gvKeyPersonnel.DataKeys[e.RowIndex].Values["id"].ToString();
            int id = Convert.ToInt32(gvKeyPersonnel.DataKeys[e.RowIndex].Value);

            TextBox Name = (TextBox)gvKeyPersonnel.Rows[e.RowIndex].FindControl("txtName");
            //    TextBox stor_address = (TextBox)gvKeyPersonnel.Rows[e.RowIndex].FindControl("txtaddress");
            //    TextBox city = (TextBox)gvKeyPersonnel.Rows[e.RowIndex].FindControl("txtcity");
            //    TextBox state = (TextBox)gvKeyPersonnel.Rows[e.RowIndex].FindControl("txtstate");
            //     TextBox zip = (TextBox)gvKeyPersonnel.Rows[e.RowIndex].FindControl("txtzip");
            conn.Open();
            SqlCommand cmd = new SqlCommand("update CMS_Key_Personnel set Name='" + Name.Text + "' where id=" + id, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            //lblmsg.BackColor = Color.Blue;
            //lblmsg.ForeColor = Color.White;
            lblmsg.Text = id + "        Updated successfully........    ";
            gvKeyPersonnel.EditIndex = -1;
            KeyPersonnel();
        }
        protected void gvKeyPersonnel_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvKeyPersonnel.EditIndex = -1;
            KeyPersonnel();
        }
        protected void gvKeyPersonnel_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = gvKeyPersonnel.DataKeys[e.RowIndex].Values["id"].ToString();
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete from CMS_Key_Personnel where id=" + id, conn);
            int result = cmd.ExecuteNonQuery();
            conn.Close();
            if (result == 1)
            {
                KeyPersonnel();
                //lblmsg.BackColor = Color.Red;
                //lblmsg.ForeColor = Color.White;
                lblmsg.Text = id + "      Deleted successfully.......    ";
            }
        }
        protected void gvKeyPersonnel_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string id = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "id"));
                Button lnkbtnresult = (Button)e.Row.FindControl("ButtonDelete");
                if (lnkbtnresult != null)
                {
                    lnkbtnresult.Attributes.Add("onclick", "javascript:return deleteConfirm('" + id + "')");
                }
            }
        }
        protected void gvKeyPersonnel_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("AddNew"))
            {
                TextBox inid = (TextBox)gvKeyPersonnel.FooterRow.FindControl("inid");
                TextBox inname = (TextBox)gvKeyPersonnel.FooterRow.FindControl("inname");
                //   TextBox inaddress = (TextBox)gvKeyPersonnel.FooterRow.FindControl("inaddress");
                //   TextBox incity = (TextBox)gvKeyPersonnel.FooterRow.FindControl("incity");
                //   TextBox instate = (TextBox)gvKeyPersonnel.FooterRow.FindControl("instate");
                //   TextBox inzip = (TextBox)gvKeyPersonnel.FooterRow.FindControl("inzip");
                conn.Open();
                SqlCommand cmd =
                    new SqlCommand(
                        "insert into CMS_Key_Personnel(id, name,) values('" + inid.Text + "', '" + inname.Text + "')", conn);
                int result = cmd.ExecuteNonQuery();
                conn.Close();
                if (result == 1)
                {
                    KeyPersonnel();
                    //lblmsg.BackColor = Color.Green;
                    //lblmsg.ForeColor = Color.White;
                    lblmsg.Text = inid.Text + "      Added successfully......    ";
                }
                else
                {
                    //lblmsg.BackColor = Color.Red;
                    //lblmsg.ForeColor = Color.White;
                    lblmsg.Text = inid.Text + " Error while adding row.....";
                }
            }
        }
    }

}


modified 14-May-15 21:42pm.

NewsRe: Update in C# using SQL Server Database. Pin
Norris Chappell15-May-15 13:39
Norris Chappell15-May-15 13:39 
Questionhow work class scheduling system by C# Pin
Member 116782469-May-15 19:54
Member 116782469-May-15 19:54 
AnswerRe: how work class scheduling system by C# Pin
OriginalGriff9-May-15 19:58
mveOriginalGriff9-May-15 19:58 
AnswerRe: how work class scheduling system by C# Pin
Afzaal Ahmad Zeeshan10-May-15 10:33
professionalAfzaal Ahmad Zeeshan10-May-15 10:33 
QuestionMethod Overload Troubles? Pin
Marcel Cartier9-May-15 16:42
Marcel Cartier9-May-15 16:42 
AnswerRe: Method Overload Troubles? Pin
OriginalGriff9-May-15 19:57
mveOriginalGriff9-May-15 19:57 
Questionwebbrowser auto tab click Pin
esancakdar8-May-15 2:17
esancakdar8-May-15 2:17 
AnswerRe: webbrowser auto tab click Pin
Dave Kreskowiak8-May-15 3:49
mveDave Kreskowiak8-May-15 3:49 
AnswerRe: webbrowser auto tab click Pin
Dr Gadgit10-May-15 4:29
Dr Gadgit10-May-15 4:29 
QuestionHow Do I Pin
Cianide8-May-15 2:00
Cianide8-May-15 2:00 
SuggestionRe: How Do I Pin
ZurdoDev8-May-15 2:20
professionalZurdoDev8-May-15 2:20 
GeneralRe: How Do I Pin
Cianide8-May-15 3:14
Cianide8-May-15 3:14 
AnswerRe: How Do I Pin
Pete O'Hanlon8-May-15 2:24
mvePete O'Hanlon8-May-15 2:24 
GeneralRe: How Do I Pin
Cianide8-May-15 2:35
Cianide8-May-15 2:35 
GeneralRe: How Do I Pin
Pete O'Hanlon8-May-15 2:37
mvePete O'Hanlon8-May-15 2:37 
GeneralRe: How Do I Pin
Cianide8-May-15 3:16
Cianide8-May-15 3:16 
GeneralRe: How Do I Pin
Mycroft Holmes8-May-15 13:52
professionalMycroft Holmes8-May-15 13:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.