Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there,

I have a web app. project in which i have added a new project to the solution for data accessing DAL it has a class SQLHelper.cs that i has referenced to my website.

Now in my SQLHelper.cs class, I Am writing this--

SqlConnection conn = GetConnection();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO tblProducts(ProductName,QtyAvailable,Rate) VALUES(@ProductName,@QtyAvailable,@Rate)";
cmd.Parameters.Add("@ProductName", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[2].Controls[0]).Text;
cmd.Parameters.Add("@QtyAvailable", SqlDbType.SmallInt).Value = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text;
cmd.Parameters.Add("@Rate", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text;

cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();


And it is showing error the name GridView1 does not exist in the current context.I have added these refrences SQLHelper.cs class--

C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts

;

whats missing guys?....
can anyone help.plz

Thanks
Amit
Posted
Comments
Sunasara Imdadhusen 17-Sep-10 2:36am    
Please make sure the control you are used in class file are exist in .aspx page?
AmitChoudhary10 17-Sep-10 2:45am    
it do

Pass the values obtained from the grid to the method in your SQLHelper.cs class via method parameters, perhaps like:

public void InsertData(string productName, int qtyAvailable, int rate)<br />
{<br />
    ... rest of code<br />
}<br />
 
Share this answer
 
Comments
AmitChoudhary10 17-Sep-10 2:40am    
But I have used dataset and parameterized it with a query

public DataSet RowInsert(string Query)
{
DataSet ds= new DataSet();
try
{
SqlConnection conn = GetConnection();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO tblProducts(ProductName,QtyAvailable,Rate) VALUES(@ProductName,@QtyAvailable,@Rate)";
cmd.Parameters.Add("@ProductName", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[2].Controls[0]).Text;
cmd.Parameters.Add("@QtyAvailable", SqlDbType.SmallInt).Value = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text;
cmd.Parameters.Add("@Rate", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text;

cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();

}
catch (Exception ex)
{ }
finally
{ DisposeConnection(); }
return ds;
}
Svetlin Panayotov 17-Sep-10 2:48am    
okay. To simplify it further...What you're trying to do looks a lot like this code:

class MyPage
{
protected string SomethingInThePage;

public void UseDAL()
{
MyDAL d = new MyDAL();
d.GetSomethingFromThePage();
}
}

class MyDAL
{
public void GetSomethingFromThePage()
{
string test = SomethingInThePage;
}
}

If you can't see why the code above will not work then you really need to read more about OOP
Dalek Dave 17-Sep-10 3:40am    
Good Answer
GridView1 is an instance of a GridView and it is on your form, not in your SQLHelper class. If you want to use it there, you must pass it to the class (via a function or constructor). This has nothing to do with references to namespaces.
 
Share this answer
 
Comments
AmitChoudhary10 17-Sep-10 2:38am    
GridView1 is the id of the grid that i have created on my form.And I am Adding parameters to it in this class using SqlCommand and givivg GridVie1 value as i have written in code above for SQLHelper.cs .
you are using GridView1 in the SQLHelper.cs class..the GridView1 is only accessible to the page.aspx.cs class...rather than to a seperate class....so restructure your code...
 
Share this answer
 
Comments
AmitChoudhary10 17-Sep-10 2:47am    
But i have referenced my DAL proj. to the website and accessing it by creating its SQLHelper class object for all methods.
senguptaamlan 17-Sep-10 2:58am    
whenever you create a control on a page...its accessible to the pages code behind only (why?? just see its access specifier)....if you want to access the grid view then pass the grid view instance to the SQLHelper class and add a namespace System.Web.UI.WebControls and a reference to the assembly System.Web. but I would like to say that you should reformat your code...cause its a good practice to consolidate the UI related code in the pages code behind class rather than splitting it to variour layers of the application.
AmitChoudhary10 17-Sep-10 5:39am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
You need to read up on variable scope. You do not get access to all variables in one class from another class simply by referencing the project the other class is in. You will have to first specify the class and then a period and then the variable/property name (e.g., SomeClass.SomeVariable). And that is IF the variable is public. With ASP.Net pages, I'm fairly certain the controls on that page (also accessible as properties) are protected by default, meaning you cannot access them from another class... you must pass it to the other class from the page class that contains the control.
 
Share this answer
 
Comments
Dalek Dave 17-Sep-10 3:44am    
Good answer.
AmitChoudhary10 17-Sep-10 5:40am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
It looks like your Gridview name is not GridView1.
Check in your aspx page and make use your gridview controls ID=GridView1.
The error doesn't have anything to do with incorrect references.

Hope this helps.
 
Share this answer
 
Comments
AmitChoudhary10 17-Sep-10 2:41am    
that is GridView1
You shouldn't access your GridView like that. Just get the values you need from the grid (in your page) and pass them as parameters to your method in SQLHelper
 
Share this answer
 
v2
Comments
AmitChoudhary10 17-Sep-10 2:44am    
i am taking all the values from the grid on my page on page load,this i am doing for rowEdit,RowInsert and rowDelete.like this

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataSet ds = objSqlHelper.RowDelete("DELETE FROM tblProducts WHERE Pid=@Pid");
BindData();
}

and here also i am getting errors in RowInsert

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900