Solved

Asp.Net/C# Cannt add new data in GridView

Posted on 2014-12-20
5
112 Views
Last Modified: 2015-02-15
Hi,
I am trying to use the following code to add a new row in a GridView component.
Visual Studio 2013 says the error is in "GetData" (no context), can somebody help?

        protected void AddNewCustomer(object sender, EventArgs e)
        {
            string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            string CustomerID = ((TextBox)GridView1.FooterRow.FindControl("txtCustomerID")).Text;
            string Name = ((TextBox)GridView1.FooterRow.FindControl("txtContactName")).Text;
            string Company = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into customers(CustomerID, ContactName, CompanyName) " +
            "values(@CustomerID, @ContactName, @CompanyName);" +
            "select CustomerID,ContactName,CompanyName from customers";
            cmd.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = CustomerID;
            cmd.Parameters.Add("@ContactName", SqlDbType.VarChar).Value = Name;
            cmd.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = Company;
            GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
        }

Open in new window

0
Comment
Question by:José Perez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40511062
Two things:

What does the definition of GetData look like?
Why aren't you cleaning up your connection object (i.e. closing/disposing it) before you leave this method. You're going to leak resources the way you currently have the code setup.
0
 
LVL 2

Author Comment

by:José Perez
ID: 40511116
That's a code I found on Internet, do you have a better way to add a row to a table using datagrid component?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40512561
For starters you don't look to be assigning your connection to your command object.

i.e.
cmd.Connection = con;

Open in new window

0
 
LVL 2

Author Comment

by:José Perez
ID: 40517708
I'm sorry, tomorrow I'll try.
0
 
LVL 14

Accepted Solution

by:
Lokesh B R earned 500 total points
ID: 40608406
Hi,

  protected void AddNewCustomer(object sender, EventArgs e)
  {
            string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            string CustomerID = ((TextBox)GridView1.FooterRow.FindControl("txtCustomerID")).Text;
            string Name = ((TextBox)GridView1.FooterRow.FindControl("txtContactName")).Text;
            string Company = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;
            using(SqlConnection con = new SqlConnection(strConnString))
            {
               con.Open();
                SqlCommand cmd = new SqlCommand();
               cm.CommandType = CommandType.Text;
               cmd.CommandText = "insert into customers(CustomerID, ContactName, CompanyName) " +
               "values(@CustomerID, @ContactName, @CompanyName);" +
               "select CustomerID,ContactName,CompanyName from customers";
               cmd.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = CustomerID;
               cmd.Parameters.Add("@ContactName", SqlDbType.VarChar).Value = Name;
               cmd.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = Company;
               cmd.ExecuteNonQuery();

                GetData();
        }
}


  protected void GetData()
  {
            string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            using(SqlConnection con = new SqlConnection(strConnString))
            {
               con.Open();
               SqlCommand cmd = new SqlCommand();
               cm.CommandType = CommandType.Text;
               cmd.CommandText = "select CustomerID,ContactName,CompanyName from customers";
               SqlDataAdapter da = new SqlAdapter(cmd);
               DataSet ds = new DataSet();
              da.Fill(ds);
               
              GridView1.DataSource = ds.Tables[0];
              GridView1.DataBind();
        }
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question