Label text is not updated in first try in asp.net. Why? - c#

On the page, I have only textbox,button,gridview and a label.
I write username to textbox and click button, so it searches in db and fills gridview and update label text as "xx result found" so xx is the count of results found in db.
the sample code is very short and easy:
string userName = TextBox2.Text;
SqlDataSource1.SelectCommand = "Select * From SampleTable Where name = '"+userName+"'";
ResultLable.Text = GridView1.Rows.Count + " Result Found";
GridView1.Visible = true;
ResultLable.Visible = true;
for example; there is a username John in db but Mary does not exist. When I search John, the count is 1 (which is correct), then I search for Mary and click button again still the number is 1(which is incorrect) but when I click button again it becomes 0. Same case valid for reverse scenario as well.
So the problem is click button is not updating the number in first click-just working for 2nd click.
Why does it happen ?

after set SelectCommand you need to call
SqlDataSource1.DataBind();
GridView1.DataBind();

The problem is that you are looking in the GridView1.Rows.Count, when you click the button the Grid is not rebind so the Rows.Count will be from previous value of the grid.
ResultLable.Text = GridView1.Rows.Count + " Result Found";
You should Rebind your Grid before taking GridView1.Rows.Count() in the Button_Click.

Related

How Can I show ViewDetail on another page in Asp.net C#

I have 18 columns in my Database. I want to show just 5 columns on screen, and others should appear on another page When I select on Select Event.
I can Show 5 columns on screen but don't know how to show reaming data on another screen.
I Have tried this method in "SelectedIndexChanged"*
GridViewRow gr = GridView1.SelectedRow;
Response.Redirect("Show.aspx?Name=" + gr.Cells[0].Text + "&Father" +
gr.Cells[1].Text + "&Gender" + gr.Cells[2]);
and this code in Another page
lblSerial.Text = Request.QueryString["Name"].ToString();
lblName.Text = Request.QueryString["Father"].ToString();
lblFather.Text = Request.QueryString["Gender"].ToString();
But by this method, I can only the data which is being shown in GridView, not the other data in the Database.

fill textbox on datagrid row click

I want to fill textboxes when a user clicks on a row. I don't want to use grdUser..SelectedRow.Cells[1].Text because my datagrid has a summary of the user details e.g. name, SSNumber, email. if I click on the row then I want all the user details should load into textboxes. How would I do that?
I would like to use a sql query but then my where clause should use SSNumber but not sure that's best practise.
C# or vb help welcome.
You can add Attributes to the Row created and when the user clicks that row redirect them to a page with the Id of the object you want to display. I know you are using a datagrid my example is using a GridView
For example on Row created
protected void ListGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType .DataRow)
{
e.Row.Attributes[ "onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';" ;
e.Row.Attributes[ "onmouseout"] = "this.style.textDecoration='none';" ;
e.Row.ToolTip = "Click to select row";
e.Row.Attributes[ "onclick"] = "Javascript:window.location = '" + ResolveClientUrl("~/PickList/ScanPickListItem.aspx" ) + "?PLRID=" + DataBinder.Eval(e.Row.DataItem, "ScreenRowId" ) + "'";
}
}
First, get the index of the selected row then get the value of the specific cell
Dim dgv_active_Row as Integer= dgv.CurrentRow.Index
TextBox1.Text = dgv.Item(1, dgv_active_Row).Value
'1' is the index of the specific column where you want to get the value of the textbox

Running Dual GridView Controls to show Summary / Details View

I've got a query to display parts by serial numbers in one column and the number of processes they went through in the other column.
This data is dumped into a GridView control called gvSummary. It works OK now.
What I'd like to do is add a second GridView control called gvDetails next to gvSummary, and replace all of the data in gvSummary with a control that will run a detail query for that particular part when it is clicked.
My plan of attack is to replace the text in the Serial_Number column (Column[0]) with something like the HyperLink control, where the NavigateUrl specifies to call my GetDetails method by passing the text of the Serial_Number.
var table = GetPartsForTimePeriod();
for (int i = 0; i < table.Rows.Count; i++) {
string serialNumber = table.Rows[i][0].ToString().Trim();
var link = new HyperLink()
{
ID = string.Format("lnk{0}", serialNumber.Replace(' ', '_'))
};
link.Text = serialNumber;
link.NavigateUrl = string.Format("GetDetails(\"{0}\"}", serialNumber);
table.Rows[i][0] = link;
}
GridView1.DataSource = table;
GridView1.DataBind();
Issues:
A HyperLink would require a page to load, and all I really want to do is fill this next query into the gvDetails control.
What is the right way to go about this? I generally code WinForms, and I can't think of the way to do this in ASP.NET.

Populating a dropdownlist with a specific value plus an array of values from an sql query

I am new to the wonderful world of dropdownlists and I know this is going to be difficult for me to explain but I will try the best I can.
Here is my data:
**Type** **ID** **Status**
Letter 1 A
Letter 2 B
Letter 3 C
Letter 4 D
Letter 5 E
From my query result I return the ID of "3" and my Drop Down List displays a "C". I have accomplished this piece already :). My question is, how can I populate the dropdownlist with the Statuses? My DDL will display a "C" and when it the arrow is clicked, the rest of the Status values will be displayed.
Is this something that can be done with the sql query? Do I have to create a separate method? If someone could point me in the right direction or provide some helpful code that would be awesome! Here is my code:
SqlDataAdapter adapter = new SqlDataAdapter("SELECT Status FROM dbo.database WHERE ID = #ID AND Type = 'Letter'", mySqlConnection1);
adapter.SelectCommand.Parameters.Add("#ID", SqlDbType.Char).Value = lblID.Text;
adapter.Fill(test);
ddlStatus.DataSource = test;
ddlStatus.DataTextField = "Status";
ddlStatus.DataValueField = Status";
ddlStatus.DataBind();
I think this is what you are looking for:
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT ID, Status FROM dbo.database WHERE Type = 'Letter'",
mySqlConnection1);
adapter.Fill(test);
ddlStatus.DataSource = test;
ddlStatus.DataTextField = "Status";
ddlStatus.DataValueField = "ID";
ddlStatus.DataBind();
ddlStatus.SelectedValue = lblID.Text;
First, fill the DropDownList with the values you need, then set the SelectedValue to have that value automatically displayed. From what I understand, you want the Status to be displayed to the user, but the actual selected value should be the ID.
Depending on your usage of ViewState, you may only need to load the DropDownList once (when the page is first loaded) and then simply set the SelectedValue after that. (You'd need to reload the list on a second page load. Use the IsPostBack value to determine whether or not to reload). There is, however, very little harm in reloading each time, other than a fraction of a second being added to your page load time.
I think what you are looking for is SelectedValue
ddlStatus.DataSource = test;
ddlStatus.DataTextField = "Status";
ddlStatus.DataValueField = "ID";
ddlStatus.DataBind();
//Add this line
string defaultValue = "3";
ddlStatus.SelectedValue = defaultValue; //C has ID of 3

How to get text boxes for Gridview Row values?

Using C# & Mysql
In my webpage am using gridview, if i click the column in the girdview the value should display in the textbox.
For Example
Griview
Column1 column2 Column3
1 Raja 9876
2 Ravi 7890
3 Ramu 9879
...
If i click the 2 rows all the values should display in the textbox
Textbox1.text = 2
textbox2.text = Ravi
textbox3.text = 9879
...,
How to write a code for this condition.
Need C# code Help
I'm assuming that by stating "[...]click the 2 rows[...]" you actually mean "click the 2nd row"; at least, this is what your last snippet suggests, since it shows only the values of the 2nd row (on a little side note: the ID's wrong there; it should be 7890).
The following code snippet shows a GridView which allows the selection of a single row, and uses an event handler in the code-behind to set the text of each TextBox to the according value in the selected row:
Page.aspx:
<asp:GridView runat="server" ID="gridView" OnSelectedIndexChanged="gridview_SelectedIndexChanged" AutoGenerateSelectButton="true"></asp:GridView>
Event handler in the code-behind file Page.aspx.cs:
void gridview_SelectedIndexChanged(object sender, EventArgs e)
{
var grid = sender as GridView;
if (grid == null) return;
//Cell[0] will be the cell with the select button; we don't need that one
Textbox1.Text = grid.SelectedRow.Cell[1].Text /* 2 */;
Textbox2.Text = grid.SelectedRow.Cell[2].Text /* Ravi */;
Textbox3.Text = grid.SelectedRow.Cell[3].Text /* 7890 */;
}
You can use an EditItemTemplate for this.
See the CodeProject article, Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List.

Categories