how to bind combobox and web service in asp.net - asp.net

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"));

Related

Replace the name of the cities with "City"

I am retrieving data from SQL Server to a grid view in ASP.Net. Below is the header of the grid view after I retrieved the data from the database.
Time| Atlanta_1| Atlanta_2| Atlanta_3|
I want to replace the name of the cities with "City" dynamically e.g.
Time| City_1| City_2| City_3|
So basically I want to replace a part of the header with "City" and I want to do it while binding it in a grid view NOT in SQL Server. Below is the code how I am retrieving data from the database to my gridview
SqlConnection con = new SqlConnection("My Connection");
string s = "My Stored Procedure";
con.Open();
SqlDataAdapter da = new SqlDataAdapter(s, con);
DataSet ds = new DataSet();
da.Fill(ds);
gridView1.DataSource = ds;
gridView1.DataBind();
con.Close();
You can use the RowDataBound event. Then loop all the Header Cells. Check the Cell for the _ and replace with the correct value. If it is always Atlanta you can just do a Replace.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
if (cell.Text.Contains("_"))
{
cell.Text = "City_" + cell.Text.Split('_')[1];
}
}
}
}

Add Session data to ListBox

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();

how to get gridview all rows data when using paging

I have a gridview with paging (page size 10), which contains Course details. when i am saving gridview data into database it save only 10 record (according to page).
is there any to get all gridview all rows.
e.g. if my gridview contains 25 rows with paging and when i save data into database then all 25 rows data insert into database?
please help...
Here is my code
DataTable dt = new DataTable();
dt.Columns.Add("row_index");
dt.Columns.Add("edited_value");
IList<int?> SeqNos = new List<int?>();
foreach (GridViewRow gvr in gvViolationCodes.Rows)
{
TextBox tb = (TextBox)gvr.FindControl("txtSeqNo");
Label lblSysid = (Label)gvr.FindControl("lblSysID");
HiddenField hf = (HiddenField)gvr.FindControl("HiddenField1");
if (tb.Text != hf.Value)
{
DataRow dr = dt.NewRow();
SeqNos.Add(Convert.ToInt32(tb.Text));
dr["row_index"] = lblSysid.Text;
dr["edited_value"] = tb.Text;
dt.Rows.Add(dr);
}
}
You need to save the data in temporary storage in PageIndexChanging event of GrIdView. On final save send the datatable to Sql Server Procedure or loop through the datatable and save it in database. Follow the steps:
protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
fnStoreGridData();
}
private void fnStoreGridData()
{
DataTable TempDataTable = new DataTable();
if(Session["Data"]!=null){
TempDataTable = Session["Data"] as DataTable;
}
else{
TempDataTable.Columns.Add("exCourse", typeof(string)) ;
TempDataTable.Columns.Add("exUniversityCourse", typeof(string)) ;
}
foreach (GridViewRow row in gvExportCourse.Rows)
{
Label exCourse = (Label)row.FindControl("gvlblCourseName");
Label exUniversityCourse = (Label)row.FindControl("gvlblUniversityCourseName");
if (exCourse != null)
{
if (!string.IsNullOrEmpty(exCourse.Text))
{
TempDataTable.Rows.Add(exCourse.Text, exUniversityCourse.Text);
//TempDataTable is your datatable object. Make sure that datatable contains these columns
}
}
}
}
In Final Save:
protected void button_Click(object s, EventArg e)
{
fnStoreGridData(); //Here this will bind the data of current page
DataTable TempDataTable = new DataTable();
if(Session["Data"]!=null){
TempDataTable = Session["Data"] as DataTable;
}
//Now loop through datatable and store the values
foreach(DataRow in TempDataTable.Rows)
{
CourseInformation course = new CourseInformation();
course.AddCourseMaster(dr["exCourse"].ToString(), dr["exUniversityCourse"].ToString());
}
label1.Text = "Course saved successfully.";
}

ASP.NET Table control - save button postback loses all my rows and cells

I have a ASP.NET Table control which I have dragged to my page. I have to create the rows and columns dynamically which I have also done. Some of the cells in my table are textboxes. I want to be able to modify the textbox values and save the changes. I have added a button within the same panel as the table but when I try to loop through the tables rows I get zero.
Why is the table not remembering my rows and cells? As I have already loaded the data once I don't want to rebuild the table each time a button is pressed. I think by creating the table each time in Page_Load I'd be going back and forth to the database server. I really want to avoid this.
You can store your dynamically created table in view that you can fetch after rendering.
SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["dbstring"].ConnectionString);
private int ctrName = 0;
Table tableName = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
tableName = new Table();
tableName.ID = "tableName";
Session["tableName"] = tableName;
ViewState["ctrName"] = ctrName;
}
ctrName = (Int32)ViewState["ctrName"];
tableName = (Table)Session["tableName"];
pnlName.Controls.Add(tableName);
}
private void GenerateName()
{
ctrName++;
TableRow row = new TableRow();
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "Txt_Name" + ctrName;
tb.Width = 150;
cell.Controls.Add(tb);
row.Cells.Add(cell);
tableName.Rows.Add(row);
Session["tableName"] = tableName;
ViewState["ctrName"] = ctrName;
}
For Access the table details:
tableName = (Table)Session["tableName"];

Handling empty databound dropdown list in ASP.Net

I have an asp.net page that has the following form fields:
Dropdown 1. This drop down receives the data via SQL data source.
Dropdown 2. Based on the selection of the drop down 1, this drop down queries to the database using SQL datasource and is populated.
A panel control that is placed below drop down list 2 and it has a set of the controls.
My problem is there may be a situation when nothing is returned from the datasource of drop down 2, I want to show an Item "No Data found" in the dropdown list 2 and simultaneously hide the panel that is placed below dropdown 2.
Can someone please let me know how this situation can be handled.
Appreciate the help.
Thanks,
Yagya
Add the following code to your dropdownlist1 selected index change event.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// Run this code when your sql datasource 2 does not return record, you can also place an IfElse condition
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Value");
DataRow row = dt.NewRow();
row[0] = "-1";
row[1] = "Data Not Found";
dt.Rows.Add(row);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "Value";
DropDownList2.DataValueField = "ID";
DropDownList2.DataBind();
}
Updated answer: ( Try this one )
You can also place it in your sqldatasource2 selecting event.
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
if (e.Arguments.TotalRowCount == 0)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Value");
DataRow row = dt.NewRow();
row[0] = "-1";
row[1] = "Data Not Found";
dt.Rows.Add(row);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "Value";
DropDownList2.DataValueField = "ID";
DropDownList2.DataBind();
}
}
The above code will add a Item to your dropdownlist having Text = "Data Not Found"

Resources