Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now