Bind Data in Gridview Without entering in database - c#

I am trying to Bind Data Inserted through any Registration Form in Grid-view.
BUT, their is No DATABASE. We Don't have any Database, So No data is Stored to retrieve. How can i Do this..???

//declare a datatable
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("id", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
//fill it with data
DataRow dr = dt.NewRow();
dr["id"] = 1;
dr["Name"] = "Name"
dt.Rows.Add(dr);
//set the datasource and bind the grid view
GridView1.DataSource = dt;
GridView1.DataBind();

Related

adding new row to existing dataset which already has values

I have a dataset where I am pulling value from the database, now after that, I want to add some more rows to it. I get four columns from the database: approverName, approverEmail, approverRole, approvalStatus
DataSet dsResult = new DataSet();
dsResult = getDataFromDatabase();
DataTable dt = new DataTable("MyTable");
dt.Columns.Add(new DataColumn("approverName", typeof(string)));
dt.Columns.Add(new DataColumn("approverEmail", typeof(string)));
dt.Columns.Add(new DataColumn("approverRole", typeof(string)));
dt.Columns.Add(new DataColumn("approvalStatus", typeof(string)));
DataRow dr = dt.NewRow();
dr["approverName"] = "John";
dr["approverEmail"] = "John#gc.com";
dr["approverRole"] = "Manager";
dr["approvalStatus"] = "Pending";
dt.Rows.Add(dr);
dsResult.Tables.Add(dt);
But after executing code I don't see this manually added value in the dataset. It only shows the value which I had after executing line 2 of code
You don't need to create another DataTable.
Append new row to existing DataTable
DataSet dsResult = new DataSet();
dsResult = getDataFromDatabase();
DataTable dt = dsResult.Tables[0]; //Get Reference to existing table
DataRow dr = dt.NewRow();
dr["approverName"] = "John";
dr["approverEmail"] = "John#gc.com";
dr["approverRole"] = "Manager";
dr["approvalStatus"] = "Pending";
dt.Rows.Add(dr);

Is there any idea to read one full column values from once data table and store it in array list?

I have tried with following code, but it is not giving correct value,
ArrayList = outTable.Select("[FILTER_FLAG]").ToList();
//outTable is my data table having values in the column name "FILTER_FLAG"
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("ID", typeof(int)));
dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
dataTable.Rows.Add(1, "Mike");
dataTable.Rows.Add(2, "Cris");
dataTable.Rows.Add(3, "Ann");
var _selector = dataTable.Rows.Cast<DataRow>().Select(x => x.Field<int>("ID")).ToList();
ArrayList list = new ArrayList(_selector);

Adding rows in data table in c#

I have created a data table in my Application start method and I am trying to add rows to this data table from my session start method. When I try to add rows, it gives an error on the data table that
the name does not exists in the current context.
I am a beginner to programming. Any help is appreciated.
Here is my code:
// application begins
void Application_Start(Object s, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("session_id", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("username", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("login_time", System.Type.GetType("System.DateTime")));
dt.Columns.Add(new DataColumn("ip_address", System.Type.GetType("System.String")));
Application["visitorTable"] = dt;
}
// browser's first visit to the page, (session starts)
void Session_Start(Object s, EventArgs e)
{
{
Application.Lock();
DataRow dr = dt.NewRow();
dr["session_id"] = (System.String)HttpContext.Current.Session.SessionID; // session id
dr["ip_address"] = Request.ServerVariables["SERVER_NAME"]; //ip-address
dt.Rows.Add(dr);
dt = (Database)Application["visitorTable"];
Application["visitorTable"] = dt;
Application.UnLock();
}
}
Check this
Dynamically create DataTable and bind to GridView in ASP.Net
it might be help you
DataTable dt;
void Application_Start(Object s, EventArgs e)
{
dt= new DataTable();
dt.Columns.Add(new DataColumn("session_id", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("username", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("login_time", System.Type.GetType("System.DateTime")));
dt.Columns.Add(new DataColumn("ip_address", System.Type.GetType("System.String")));
Application["visitorTable"] = dt;
}
You should specify the table name when creating a new DataTabe...
DataTable dt = new DataTable("VisitorTable");
dt.Columns.Add(new DataColumn("session_id", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("username", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("login_time", System.Type.GetType("System.DateTime")));
dt.Columns.Add(new DataColumn("ip_address", System.Type.GetType("System.String")));
Application["visitorTable"] = dt;
// you create it like this
Application["visitorTable"] = dt;
// and you read it like this
dt = (Database)Application["VisitorTable"];
Notice the issue?
Also you don't need to rely on reflection to get a type's description, a simple compile time typeof(string) and typeof(DateTime) will take care of it.
when your are executing following code
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("session_id", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("username", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("login_time", System.Type.GetType("System.DateTime")));
dt.Columns.Add(new DataColumn("ip_address", System.Type.GetType("System.String")));
have you checked whether your dt added with column name specified ?
or dt is generated with columns Column1,colum2,column3
if those are generated with Column1,colum2,column3 ths why you ll be facing above error ,
rename column with your name of dt

Data Table Getting error when i add data rows[]

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Date", typeof(String)));
dt.Columns.Add(new DataColumn("Time", typeof(String)));
dt.Columns.Add(new DataColumn("Function", typeof(String)));
dt.Columns.Add(new DataColumn("Log Level", typeof(String)));
dt.Columns.Add(new DataColumn("Message", typeof(String)));
When i query DataTable then get DataRows[]
DataRow[] result = dt.Select("Function ='" + strfunction + "'");
Now i want to add that result in DataTable
dt.Rows.Add(result);
But i am getting error
Input array is longer than the number of columns in this table.
try this
foreach (DataRow row in result)
dt.Rows.Add(row);
DataRowCollection.Add() has two overloads:
Add(DatarRow row);
Add(params object[] values);
The one you are currently using is Add(params object[] values); since you are passing an array of a class with base class of object. So you are searching in the same table, getting the same number of columns indeed, but the problem is that; you expect each element of the array to be inserted as a DataRow, but there is only one row inserted and each element of the array DataRow[] result is assigned for columns
With:
dt.Rows.Add(new object[] { "Date", "Time", "a", "Log Level", "Message" });
DataRow[] result = dt.Select("Function ='a'");
foreach (DataRow r in dt.Rows)
{
/* Log r or inpect */
}
No exception was thrown in my test because the result array had only 1 element (as search result).
If i have more then 5 elements in result; since i can't insert 6 column values in a 5 column table it will throw exception.
And the correct way to do it is interating search results array and adding a new row for each array element; using DataRow.ItemArray property
You can try the following which worked for me:
DataRow[] result = dt.Select("Function ='" + strfunction + "'");
foreach (DataRow r in result)
{
dt.Rows.Add(r.ItemArray);
}
Instead of directly adding the DataRow to the DataTable, how about creating a foreach loop and creating a separate DataRow from the DataTable, filling the data and adding it into the DataTable. For example:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Date", typeof(String)));
dt.Columns.Add(new DataColumn("Time", typeof(String)));
dt.Columns.Add(new DataColumn("Function", typeof(String)));
dt.Columns.Add(new DataColumn("Log Level", typeof(String)));
dt.Columns.Add(new DataColumn("Message", typeof(String)));
//Dummy data added
DataRow dr = dt.NewRow();
dr[0] = "aa";
dr[1] = "bb";
dr[2] = "cc";
dr[3] = "dd";
dr[4] = "ee";
dt.Rows.Add(dr);
string strfunction = "cc";
DataRow[] result = dt.Select("Function ='" + strfunction + "'");
//Initialize Datarow here. I am using the one which is defined above
dr = null;
foreach (var item in result)
{
dr = dt.NewRow();
dr[0] = item[0];
dr[1] = item[1];
dr[2] = item[2];
dr[3] = item[3];
dr[4] = item[4];
dt.Rows.Add(dr);
}
Hope this helps.

Sqlbulkcopy doesn't seem to work for me

I have created a datatable and trying to insert that datatable through SqlBulkCopy but somehow it doesn't seem to work for me....
I got the error,
The given value of type DateTime from the data source cannot be converted
to type decimal of the specified target column.
My Datasource is,
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("EmpId", typeof(Int64)));
dt.Columns.Add(new DataColumn("FromDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("ToDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("DaysPresent", typeof(decimal)));
dt.Columns.Add(new DataColumn("OpeningAdvance", typeof(double)));
dt.Columns.Add(new DataColumn("AdvanceDeducted", typeof(double)));
dt.Columns.Add(new DataColumn("RemainingAdvance", typeof(double)));
dt.Columns.Add(new DataColumn("SalaryGiven", typeof(double)));
dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
foreach (GridViewRow row in gridEmployee.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
DataRow dr = dt.NewRow();
dr["EmpId"] = Convert.ToInt64(((HiddenField)row.Cells[0].FindControl("HiddenId")).Value);
dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
dr["DaysPresent"] = Convert.ToDecimal(((TextBox)row.Cells[3].FindControl("TxtDaysPresent")).Text);
dr["OpeningAdvance"] = Convert.ToDouble(((TextBox)row.Cells[4].FindControl("txtOpeningAdv")).Text);
dr["AdvanceDeducted"] = Convert.ToDouble(((TextBox)row.Cells[5].FindControl("TxtAdvanceDeducted")).Text);
dr["RemainingAdvance"] = Convert.ToDouble(((TextBox)row.Cells[6].FindControl("TxtClosingAdvance")).Text);
dr["SalaryGiven"] = Convert.ToDouble(((TextBox)row.Cells[7].FindControl("TxtSalary")).Text);
dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
dt.Rows.Add(dr);
}
}
SqlBulkCopy sbc = new SqlBulkCopy(connectionString);
sbc.DestinationTableName = "SalaryDetails";
sbc.WriteToServer(dt);
sbc.Close();
And my destination Table looks like this,
alt text http://img231.imageshack.us/img231/5448/mytable.jpg
You need to specify a column mapping, as you don't have the same number of columns in the DataTable and the destination table.
You can do it like this:
sbc.ColumnMappings.Add("EmpId", "EmpId");
sbc.ColumnMappings.Add("FromDate", "FromDate");
// and so on, with the rest of the columns; and after that comes
sbc.WriteToServer(dt);
sbc.Close();

Categories

Resources