I didn't write all code. It just a part of sessions which has already data inside. I just want to get all data from sessiosn by which Session["sepet"] to add Listbox but I couldn't do that. There is no any error message. Actually the code purpose is, I wanted to add all data to Listbox after that I want to send all data what include the Listbox to SQL Database. I don't know is there any different way to do.
private void SepetGetir1()
{
List<string> lst = new List<string>();
if (Session["sepet"] != null)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["sepet"];
foreach (DataRow r in dt.Rows)
{
lst.Add(r["ID"].ToString());
lst.Add(r["productName"].ToString());
}
ListBox1.DataSource = lst;
}
}
You can bind the data table to the ListBox, and specify which field is used for Text and wihch one is used for Value:
DataTable dt = Session["sepet"] as DataTable;
ListBox1.DataSource = dt;
ListBox1.DataTextField = "productname";
ListBox1.DataValueField = "ID";
ListBox1.DataBind();
Related
I have a combo box and a web service. I want to call web service and then when I open my combo I see the values the are in my web service.My code did not work.Can anybody help me?
protected void Page_Load(object sender, EventArgs e)
{
string firstParam = "1";
DataSet ds2 = ws1.Automation_H022_MamooriyatSaati_View(firstParam, secondParam, thirdParam);
this.GridView1.DataSource = ds2;
foreach (System.Data.DataTable table1 in ds1.Tables)
{
foreach (DataRow dr in table1.Rows)
{
cmbox.DataTextField = "Tp_Place";
cmbox.DataValueField = "Lg_Personality";
cmbox.DataBind();
cmbox.Items.Insert(0, new ListItem("", "1"));
}
}
}
You are adding not adding anything to the cmbox in the foreach (DataRow dr in table1.Rows). And even if you did the next loop would overwrite it.
You need to set the datasource for the ComboBox in the same way you set the GridView.
If there is more than one table in ds1 and you want both tables to be visible in the combobox then you need to merge those two (or more) tables first and then bind them as a single source of data.
DataSet ds2 = ws1.Automation_H022_MamooriyatSaati_View(firstParam, secondParam, thirdParam);
this.GridView1.DataSource = ds2;
cmbox.DataSource = ds1.Tables[0];
cmbox.DataTextField = "Tp_Place";
cmbox.DataValueField = "Lg_Personality";
cmbox.DataBind();
cmbox.Items.Insert(0, new ListItem("", "1"));
I have added records chosen (checkbox) in a datatable shown below, records have been displaying in a datagrid populated using stored procedure. Then on the button click I save all the selected (added) records to the ds in database.
public DataSet getMain()
{
DataSet ds = CFObj.GetSchemaRequest();
DataRow dr;
dr = ds.Tables[0].NewRow();
for (int i = 0; i < dgAvailableCandidates.Items.Count; i++)
{
CheckBox saveChkBoxItem = (CheckBox)dgAvailableCandidates.Items[i].FindControl("chkbox_SelectCandidate");
if (saveChkBoxItem.Checked)
{
//ds.Tables[0].Columns["pk_scrndRecId"].ReadOnly = false;
dr["pk_scrndRecId"] = "0";
dr["fk_jobId"] = ddl_jobList.SelectedValue.ToString();
obj._fkjob_Id = ddl_jobList.SelectedValue.ToString();
dr["fk_recId"] = dgAvailableCandidates.DataKeys[i].ToString();
obj._fk_recId = dgAvailableCandidates.DataKeys[i].ToString();
}
}
ds.Tables[0].Rows.Add(dr);
return ds;
}
here my issue is when ds.Tables[0].Rows.Add(dr); i put outside of loop, it adds only one last selected record to ds, and when I put this inside loop, it throws following exception
[pk_scrndRecId] column is read only
and when I have placed this inside if block, it throws following exception:
This row already belongs to this table.
I am not able to understand why this code doesn't add all the selected rows of datagrid to the ds.
please help. thanks!
You have to create a new row and add it to datatable inside the loop and so each time it will create a new instance and add it to your datatable.
Check updated code.
public DataSet getMain()
{
DataSet ds = CFObj.GetSchemaRequest();
DataRow dr;
for (int i = 0; i < dgAvailableCandidates.Items.Count; i++)
{
CheckBox saveChkBoxItem = (CheckBox)dgAvailableCandidates.Items[i].FindControl("chkbox_SelectCandidate");
if (saveChkBoxItem.Checked)
{
dr = ds.Tables[0].NewRow();
//ds.Tables[0].Columns["pk_scrndRecId"].ReadOnly = false;
dr["pk_scrndRecId"] = "0";
dr["fk_jobId"] = ddl_jobList.SelectedValue.ToString();
obj._fkjob_Id = ddl_jobList.SelectedValue.ToString();
dr["fk_recId"] = dgAvailableCandidates.DataKeys[i].ToString();
obj._fk_recId = dgAvailableCandidates.DataKeys[i].ToString();
ds.Tables[0].Rows.Add(dr);
}
}
return ds;
}
I am doing an online ordering system. I want to check if the item exist in the gridview so it will not add another row or have multiple lines (It will update the qty and the price). The gridview is updating using dgvOrder.Rows[i].Cells[2].Text but my problem is how to update the datatable.
bool isExist = false;
if (Session["dtInSession"] != null)
dt = (DataTable)Session["dtInSession"]; //Getting datatable from session
for (int i = 0; i < dgvOrder.Rows.Count; i++)
{
if (dgvOrder.Rows[i].Cells[0].Text == b.ID)
{
isExist = true;
dgvOrder.Rows[i].Cells[2].Text = Convert.ToString(Convert.ToInt32(dgvOrder.Rows[i].Cells[2].Text) + 1);
dgvOrder.Rows[i].Cells[3].Text = Convert.ToString(Convert.ToInt32(dgvOrder.Rows[i].Cells[2].Text) * price);
}
}
if (!isExist)
{
DataRow dr = dt.NewRow();
dr["pCode"] = b.ID;
dr["desc"] = description;
dr["qty"] = "1";
dr["price"] = price;
dt.Rows.Add(dr);
dgvOrder.DataSource = dt;
dgvOrder.DataBind();
}
Because I am planning to pass the datatable using the session variable.
Session["orders"] = dt;
Response.Redirect("FinalizeOrder.aspx");
after passing the data, the code is working but only 1 qty for each item is passing.
Can anyone help me in this issue?
The problem is that you reload the DataSource of the GridView only if !isExist. Otherwise you are changing the datatable, but you are not assigning it to the GridView again, so the values are coming from the ViewState.
So this should work:
// update the table
// ...
if (!isExist)
{
// ...
}
// reassign the updated DataTable and DataBind the grid always
dgvOrder.DataSource = dt;
dgvOrder.DataBind();
My dropdownlist is set to databine like this...
dt = dal.FillDataTable(SqlConnectionString, "SELECT SQL Query Statement")
dropdownlist1.datasource = dt
dropdownlist1.datatextfield = dt.columns.item(0).tostring
dropdownlist1.databind()
This is turn populates my dropdownlist, when a user selects a value, it is then populated to the remaining textboxes on the remaining forms with a session call...
dropdownlist2.add(ctype(session.item("valOne"), String))
Through this session it populates the one value, is it possible to display the selected value but also include all other dropdownlist items in case they want to change thier selection? Any suggestions would really help?
I didnt understand adding one value at a time. Just show them all the associated values and let them select or change their decision.
Sample code
public DataSet GetmTest_Filter()
{
try
{
DataSet oDS = new DataSet();
SqlParameter[] oParam = new SqlParameter[1];
oParam[0] = new SqlParameter("#col_Id", _scolidvalue);
oDS = SqlHelper.ExecuteDataset(DataConnectionString, CommandType.StoredProcedure, "your_stored_procedure_here", oParam);
return oDS;
}
catch (Exception e)
{
ErrorMessage = e.Message;
return null;
}
}
public void ddlFill_Test(DropDownList ddl)
{
DataSet oDSddlmTest = new DataSet();
oDSddlmTest = GetmTest_Filter();
if (oDSddlmTest.Tables[0].Rows.Count > 0)
{
ddl.DataSource = oDSddlmTest.Tables[0].DefaultView;
ddl.DataTextField = "col_desc";
ddl.DataValueField = "col_id";
ddl.DataBind();
}
else
{
ddl.Enabled = false;
}
}
May this help you.
what can I do when my all database table's name into dropdown list & when i select or click the table name from dropdown list display the whole field into the gridview dynamically..please give me best solution in asp.net.
Use the mysql commands SHOW TABLES and DESCRIBE table_name and draw the result into grids. I don't know the ASP codes.
(This is an example for MSSQL db. For mysql you can try using OdbcConnection or OleDbConnection depending on MySql version.)
1.Here is direct way to select data:
public class DAL
{
public static DataTable GetTableData(string tableName)
{
DataTable dt = new DataTable();
using (SqlConnection cn = new SqlConnection(Settings.Default.ConnectionString))
{
cn.Open();
SqlCommand cmd = cn.CreateCommand();
cmd.CommandText = string.Format("select * from {0}", tableName);
using (SqlDataReader rd = cmd.ExecuteReader())
{
bool ColumnsAdded = false;
while (rd.Read())
{
if (!ColumnsAdded)
{
for (int i = 0; i < rd.FieldCount; i++)
dt.Columns.Add(rd.GetName(i), rd.GetFieldType(i));
}
DataRow row = dt.NewRow();
for (int i = 0; i < rd.FieldCount; i++)
row[i] = rd[i];
dt.Rows.Add(row);
ColumnsAdded = true;
}
}
}
return dt;
}
}
2.Next you drop ObjectDataSource onto your form with a GridView (setting its DataSourceID to objectdatasource). Specify this method on your ObjectDataSource select method. You can specify parameter for tableName to be read from Control (DropDownList1 for instance). DropDownList1 presumebly keeps a list of tables.
You can dynamically load the table contents into gridview by putting a select query for the table and then bind that dataset to gridview datasource.
Hope this helps you!!