How Can I show ViewDetail on another page in Asp.net C# - 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.

Related

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

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.

Search DataSet and Display Multiple Results(Rows) in TextBoxes

I would like to search my DataSet for a customer name, this name will occur more than once throughout the dataset, meaning there will be more than 1 result (multiple rows returned)
When my search button is clicked, form2 opens up, and in theory should display all rows containing the customer name, but how do I achive this?
I can get it to display 1 row without a problem, but how would I make it display all rows, and the data in textboxes.
At the moment I am using a foreach loop to fill text boxes with returned data, but this only takes data from 1 row within my DataSet.
Is there a way I can make my form auto generate textboxes and populate them will all the data from the array? When I run my query at the moment, it fills the textboxes on form2 with the last rotation of the foreach.
DB Sample
Appreciate any help
Adam
DataRow[] returnedRows;
returnedRows = ds.Tables["Table_Data_1"].Select("cust_name='" + searchFor + "'");
foreach (DataRow returned in returnedRows)
{
tbName.Text = returned[1].ToString();
tbAddress.Text = returned[2].ToString();
tbPhone.Text = returned[3].ToString();
tbMake.Text = returned[4].ToString();
tbModel.Text = returned[5].ToString();
tbReg.Text = returned[6].ToString();
tbYear.Text = returned[7].ToString();
tbParts1.Text = returned[8].ToString();
tbParts2.Text = returned[9].ToString();
tbParts3.Text = returned[10].ToString();
tbParts3.Text = returned[11].ToString();
}
The reason you're having just a single value appear is that you're setting the text of your text boxes to a new value for each row that you've selected. You could change your code to instead add to the Text in the the Textboxes:
tbName.Text += returned[1].ToString() + Environment.NewLine;
Or you could instead bind it to a DataGridView, which is designed to display tabular data. Assuming the DataGridView was named something like customerDataView,
returnedRows = ds.Tables["Table_Data_1"].Select("cust_name='" + searchFor + "'");
var newTable = returnedRows.CopyToDataTable();
BindingSource bindSource = new BindingSource();
bindSource.DataSource = newTable;
var customerDataView.DataSource = bindSource;
You can generate textboxes dinamicaly
Its something like this
foreach (DataRow returned in returnedRows)
{
TextBox txt = new TextBox();
txt.ID = "textBox1"; //generate id dinamically can be a count
txt.Text = returned[1].ToString();
form1.Controls.Add(txt);
}
The way you've implemented the loop override the data in each interation
I've improved the sample code from following this post
https://stackoverflow.com/a/2229040/5252904 answer provided by #rahul.

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.

Filling a DataTable based on row values in a DataSet

I want to show some news posts on the front page of a website. The news have different categories but are stored in the same mysql database. All news items are loaded into a dataset but from there I just want to extract some specific news and show them in a ListView control.
At the moment I'am using the workaround solution of using a foreach loop on the dataset and formatting the result with html in the code behind. It works but I want all html formatting to be done in the aspx file, thus I'am looking for a better solution.
DataSet articles = db.loadAllArticles();
foreach (DataRow item in articles.Tables["articles"].Select("category = 1"))
{
result += "<h1 class='headline'>" + item["headline"] + "</h1><h2 class='introduction'>" + item["introduction"] + "</h2><p class='content'>" + item["content"] + "</p><p class='authorAndDate'>" + item["author"] + " " + item["datePosted"].ToString().Substring(0,10) + "</p><br/>";
}
lblDisplay.Text = result;
What I was hoping I could do was simply something like this:
DataSet articles = db.loadAllArticles();
ListView1.DataSource = articles.Tables["articles"].Select("category = 1");
ListView1.DataBind();
But the ListView control is not too happy about trying to bind DataRow to it or something.
The best workaround I can think of is to create a new Table within the "articles" DataSet which contains only articles of the chosen category, so something like this:
DataSet articles = db.loadAllArticles();
articles.Tables.Add("frontPageArticles");
articles.Tables["frontPageArticles"] = ???
And then thats where it stops. How can I fill the new datatable with rows from the other datatable where the value of a column is x?
-Eric.
You should be looking at binding you ListView to a DataView, it is filterable and sortable.
You can design your web form using control like repeater, datalist etc. and can bind these control to your datatable on codebehind.
An example can be found here

Categories