Click here to Skip to main content
15,888,816 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to implement Save As functionality in C# Pin
Dave Kreskowiak22-May-15 4:49
mveDave Kreskowiak22-May-15 4:49 
AnswerRe: How to implement Save As functionality in C# Pin
Eddy Vluggen22-May-15 4:23
professionalEddy Vluggen22-May-15 4:23 
GeneralRe: How to implement Save As functionality in C# Pin
RajuPrasad8222-May-15 5:49
professionalRajuPrasad8222-May-15 5:49 
GeneralRe: How to implement Save As functionality in C# Pin
Sascha Lefèvre22-May-15 5:57
professionalSascha Lefèvre22-May-15 5:57 
GeneralRe: How to implement Save As functionality in C# Pin
RajuPrasad8222-May-15 6:01
professionalRajuPrasad8222-May-15 6:01 
GeneralRe: How to implement Save As functionality in C# Pin
Sascha Lefèvre22-May-15 6:33
professionalSascha Lefèvre22-May-15 6:33 
AnswerRe: How to implement Save As functionality in C# Pin
CHill6022-May-15 8:14
mveCHill6022-May-15 8:14 
Questionobject reference not set to an instance of an object. Pin
abdo.kouta22-May-15 3:31
abdo.kouta22-May-15 3:31 
hello again guys thanks for ur useful answer today i'm going to ask about this message that i get stuck with here is my code
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Default3 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Data"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadUsersData();
        }
    }
    protected void loadUsersData()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from UsersData", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        int count = ds.Tables[0].Rows.Count;
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gridView.DataSource = ds;
            gridView.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gridView.DataSource = ds;
            gridView.DataBind();
            int columncount = gridView.Rows[0].Cells.Count;
            lblmsg.Text = " No data found !!!";
        }
    }
    protected void gridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gridView.EditIndex = e.NewEditIndex;
        loadUsersData();
    }
    protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = gridView.DataKeys[e.RowIndex].Values["id"].ToString();
        TextBox UserName = (TextBox)gridView.Rows[e.RowIndex].FindControl("txtUserName");
        TextBox Password = (TextBox)gridView.Rows[e.RowIndex].FindControl("txtPassword");
        con.Open();
        SqlCommand cmd = new SqlCommand("update UsersData set UserName='" + UserName.Text + "', Password='" + Password.Text + "' where id=" + id, con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblmsg.BackColor = Color.Blue;
        lblmsg.ForeColor = Color.White;
        lblmsg.Text = id + "        Updated successfully........    ";
        gridView.EditIndex = -1;
        loadUsersData();
    }
    protected void gridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gridView.EditIndex = -1;
        loadUsersData();
    }
    protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = gridView.DataKeys[e.RowIndex].Values["id"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from UsersData where id=" + id, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            loadUsersData();
            lblmsg.BackColor = Color.Red;
            lblmsg.ForeColor = Color.White;
            lblmsg.Text = id + "      Deleted successfully.......    ";
        }
    }
    protected void gridView_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 gridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox inid = (TextBox)gridView.FooterRow.FindControl("inid");
            TextBox inUserName = (TextBox)gridView.FooterRow.FindControl("inUserName");
            TextBox inPassword = (TextBox)gridView.FooterRow.FindControl("inPassword");
            con.Open();
            SqlCommand cmd =
                new SqlCommand(
                    "insert into UsersData(id,UserName,Password) values('" + inid.Text + "','" +
                    inUserName.Text + "','" + inPassword.Text + "')", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                loadUsersData();
                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.....";
            }
        }
    }

    protected void gridView_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

thanks..
GeneralRe: object reference not set to an instance of an object. Pin
Sascha Lefèvre22-May-15 3:55
professionalSascha Lefèvre22-May-15 3:55 
GeneralRe: object reference not set to an instance of an object. Pin
abdo.kouta22-May-15 4:13
abdo.kouta22-May-15 4:13 
AnswerRe: object reference not set to an instance of an object. Pin
OriginalGriff22-May-15 4:26
mveOriginalGriff22-May-15 4:26 
GeneralRe: object reference not set to an instance of an object. Pin
Eddy Vluggen22-May-15 4:36
professionalEddy Vluggen22-May-15 4:36 
SuggestionRe: object reference not set to an instance of an object. Pin
Richard Deeming22-May-15 5:08
mveRichard Deeming22-May-15 5:08 
GeneralRe: object reference not set to an instance of an object. Pin
Eddy Vluggen22-May-15 6:37
professionalEddy Vluggen22-May-15 6:37 
QuestionC# Stops Executing When Opening A Connection To MySql Pin
Member 1171127722-May-15 2:28
Member 1171127722-May-15 2:28 
AnswerRe: C# Stops Executing When Opening A Connection To MySql Pin
Sascha Lefèvre22-May-15 2:42
professionalSascha Lefèvre22-May-15 2:42 
GeneralRe: C# Stops Executing When Opening A Connection To MySql Pin
Member 1171127722-May-15 2:51
Member 1171127722-May-15 2:51 
GeneralRe: C# Stops Executing When Opening A Connection To MySql Pin
Sascha Lefèvre22-May-15 3:17
professionalSascha Lefèvre22-May-15 3:17 
GeneralRe: C# Stops Executing When Opening A Connection To MySql Pin
Member 1171127722-May-15 3:22
Member 1171127722-May-15 3:22 
GeneralRe: C# Stops Executing When Opening A Connection To MySql Pin
Sascha Lefèvre22-May-15 3:29
professionalSascha Lefèvre22-May-15 3:29 
GeneralRe: C# Stops Executing When Opening A Connection To MySql Pin
Member 1171127722-May-15 3:47
Member 1171127722-May-15 3:47 
GeneralRe: C# Stops Executing When Opening A Connection To MySql Pin
Sascha Lefèvre22-May-15 3:57
professionalSascha Lefèvre22-May-15 3:57 
GeneralRe: C# Stops Executing When Opening A Connection To MySql Pin
Member 1171127722-May-15 3:59
Member 1171127722-May-15 3:59 
GeneralRe: C# Stops Executing When Opening A Connection To MySql Pin
Sascha Lefèvre22-May-15 4:12
professionalSascha Lefèvre22-May-15 4:12 
GeneralRe: C# Stops Executing When Opening A Connection To MySql Pin
Member 1171127722-May-15 4:15
Member 1171127722-May-15 4:15 

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.