|
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("connection string");
DataSet ds = new DataSet();
DataTable dt = new DataTable("tblcheck");
DataColumn dc1=new DataColumn("col1",typeof(int));
dt.Columns.Add(dc1);
DataColumn dc2=new DataColumn("col2",typeof(int));
dt.Columns.Add(dc2);
//DataRow dr1=new DataRow();
DataRow dr1 = dt.NewRow();
dr1("col1")=1;
dr1("col2")=2;
dt.Rows.Add(dr1);
SqlDataAdapter adp2 = new SqlDataAdapter("select * from tblcheck", con);
adp2.Fill(ds, "tblcheck");
GridView1.DataSource = ds;
GridView1.DataBind();
}
error is
"dr1 is a variable but is used like a method."
tel me how to handle it?
Chohan
|
|
|
|
|
chohanpk wrote: dr1("col1")=1;
dr1("col2")=2;
Needs to be:
dr1["col1"]=1;
dr1["col2"]=2;
Jon Sagara
I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So i had to leave the place as soon as possible.
--Mr.Prakash
Blog | Site | Articles
|
|
|
|
|
after doing this correction ,now when executing error comes
"Invalid object name 'tblcheck'. "
on
adp2.Fill(ds, "tblcheck");
Chohan
|
|
|
|
|
it's so clear that no table 'tblcheck' in your connection object...
<< >>
|
|
|
|
|
yes alredy no table exist i nnorthwind with this name.
but with this command i m creating new table n also create columns n rows and add data in it.
DataTable dt = new DataTable("tblcheck");
DataColumn dc1=new DataColumn("col1",typeof(int));
dt.Columns.Add(dc1);
DataColumn dc2=new DataColumn("col2",typeof(int));
dt.Columns.Add(dc2);
DataRow dr1 = dt.NewRow();
dr1["col1"]=1;
dr1["col2"]=2;
dt.Rows.Add(dr1);
SqlDataAdapter adp2 = new SqlDataAdapter("select * from tblcheck", con);
adp2.Fill(ds, "tblcheck");
GridView1.DataSource = ds;
GridView1.DataBind();
Chohan
|
|
|
|
|
yes..you create a datatable dt...but that does not mean it will be created in your DB...that's why...you know?
<< >>
|
|
|
|
|
chohanpk wrote: SqlDataAdapter adp2 = new SqlDataAdapter("select * from tblcheck", con);
adp2.Fill(ds, "tblcheck");
GridView1.DataSource = ds;
GridView1.DataBind();
This is looking for a table in your database, creating one in local memory, with nothing in it, is not going to do anything. Even if you were examining the table you created ( in memory, not in you DB ), it would still be empty.
What are you hoping to achieve here ?
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
here is code:
DataTable dt = new DataTable("tblcheck");
DataColumn dc1 = new DataColumn("col1", typeof(int));
dt.Columns.Add(dc1);
DataColumn dc2 = new DataColumn("col2", typeof(int));
dt.Columns.Add(dc2);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
DataRow dr1 = dt.NewRow();
dr1["col1"] = 1;
dr1["col2"] = 2;
dt.Rows.Add(dr1);
//SqlDataAdapter adp2 = new SqlDataAdapter("select col1 from dt", con);
//adp2.Fill(ds, "dt");
GridView1.DataSource = ds;
GridView1.DataBind();
if i dont nclude these rows,
"SqlDataAdapter adp2 = new SqlDataAdapter("select col1 from dt", con);
adp2.Fill(ds, "dt");"
then it shows values.
but with these dataadapter code it gives error.i want to use dataadpater to fill the dataset.what will be the solution?
Chohan
|
|
|
|
|
chohanpk wrote: .i want to use dataadpater to fill the dataset
OK, then you need to point the dataadapter to a table that exists
chohanpk wrote: select col1 from dt
OK, that plain won't work. In your SQL, you're pointing to objects in your database, not local variables.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
tel me how can i do this? wht ll be the code?
Chohan
|
|
|
|
|
What you're trying to do, makes no sense. What do you want to achieve ? Are you hoping to create a table in the DB ? Are you hoping to create a local table and use SQL on it ? You can specify a dataview, which operates SQL on a datatable, at least, I think you can. why don't you just create a table in your database ?
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
i ve to do it with dataview.but i want to know how i can add it to db n then display through dataadapter.
Chohan
|
|
|
|
|
Well, wait a minute. You don't want to write code that adds tables at random, do you ?
you need to write the SQL to create the table, and execute it, through a connection, probably calling ExecuteNonQuery, as you want nothing back. Then you need to run some inserts, if you want some data to select from.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
yeah.i want to add tables,insert columns ,rows and then display data of that tables.
Chohan
|
|
|
|
|
Why would you want to write a program that keeps adding tables to your database ? What for ?
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
now for learning purpose
Chohan
|
|
|
|
|
OK, what you've hopefully learned is, create your tables in SQL, then query them using SQL in your C# code.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Hi friends,
In my C#.Net application i am trying to sort the gridview contents by using the following line.
dgAddressBook.Sort(dgAddressBook.Columns[23], ListSortDirection.Ascending);
but i am receiving the following error. Please help me to resolve this problem.
Error:
The original exception is: DataGridView control must be bound to an IBindingList object to be sorted. at System.Windows.Forms.DataGridView.SortDataBoundDataGridView_PerformCheck(DataGridViewColumn dataGridViewColumn)"
Thanks in Advance,
Regards,
Prya
|
|
|
|
|
this is asp.net forum ....
|
|
|
|
|
|
|
Hi all
I add usercontrol in code behide but it not run some event in (file ascx)
UserControl control = (UserControl)Page.LoadControl("tool/WebUserControl1.ascx");
Page.Controls.Add(control);
Can u show me how is postback to server on file ascx
|
|
|
|
|
|
Hi,
I have this project it has a folder that it's supposed to read but when I run an error occurs the type or namespace name src could not be found(areyou missing a using directive or an assembly referrence?).
This folder is src. What could this be?
Thanks
|
|
|
|
|
hi there:
is this compilation error not run-time?
<< >>
|
|
|
|