Solved

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

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

17 Experts available now in Live!

Get 1:1 Help Now