Solved

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

Posted on 2014-12-20
5
108 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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