?
Solved

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

Posted on 2014-12-20
5
Medium Priority
?
115 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 15

Accepted Solution

by:
Lokesh B R earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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