Reputation: 482
(this is another question, since my original post, I asked too many questions in one)
Let me state this first. I am pretty much completely new to ASP coding. I am working on a little side project that requires me to use ASP instead of PHP that I sort of did before. I have been looking at solutions for past 2 days, and have tried many things, but can't seem to get it to work with my code. I have been part of this site for some time, so I do know how it works. I would not be asking here if I wasn't already trying to do this on my own for some time. I have learned a huge amount of information about SQL on here, so I hope to do the same with ASP.
Question:
When the page initially loads, it has only a TextBox1 and a Button. If I don't enter anything in the box and hit the button, it will load my GridView with all the Data rows from the SQL Select. When I do hit the button, I would like to display the amount of rows that has been returned next to the button.
GridView1 :
<asp:TextBox ID="TextBox1" runat="server" Width="265px" Height="22px" CssClass="myBox"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search Fields" CssClass="myButton" />
<asp:GridView ID="GridView1" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting" runat="server" AutoGenerateColumns="true" CellPadding="4" EnableModelValidation="True" EnableTheming="True" ForeColor="#333333" GridLines="None" Width="100%" style="margin-top: 0px; text-align: center;" AllowPaging="True" AllowSorting="True" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
Code Behind (DB name and Password taken out):
SqlConnection vid = new SqlConnection("Data Source=ENF;Initial Catalog=***Database Name***;Persist Security Info=True;User ID=sa;Password=***Password***");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String str = "SELECT ab.NAME as [Customer] ,ISNULL(ab.TELEPHONE1,'') as [Phone #] ,ISNULL(pb.NAME,'') as [Product] ,ISNULL(aeb.NEW_PRODUCTVERSION,'') as [Version] ,CASE WHEN ab.STATUSCODE = 1 THEN 'Active' ELSE 'Inactive' END as [Status] ,ISNULL('Sal : ' + c.SALUTATION + ' / ','') + ISNULL('Title : ' + c.JOBTITLE + ' / ','') + ISNULL(a.PRIMARYCONTACTIDNAME,'') as [Primary Contact] ,ISNULL(c.TELEPHONE1,'') as [Contact Phone] FROM ACCOUNTBASE ab LEFT JOIN ACCOUNTEXTENSIONBASE aeb on ab.ACCOUNTID = aeb.ACCOUNTID LEFT JOIN PRODUCTBASE pb on aeb.NEW_PRIMARYPRODUCTID = pb.PRODUCTID LEFT JOIN ACCOUNT a on ab.ACCOUNTID = a.ACCOUNTID LEFT JOIN CONTACT c on a.PRIMARYCONTACTID = c.CONTACTID WHERE ((ab.NAME LIKE '%' + @search + '%') OR (aeb.NEW_PRODUCTVERSION LIKE '%' + @search + '%') OR (pb.NAME LIKE '%' + @search + '%') OR (a.PRIMARYCONTACTIDNAME LIKE '%' + @search + '%')) ORDER BY ab.NAME";
SqlCommand xp = new SqlCommand(str, vid);
xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;
vid.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, "Name");
GridView1.DataSource = ds;
GridView1.DataBind();
vid.Close();
}
Upvotes: 1
Views: 4251
Reputation: 27474
If you don't want to show the count until the user clicks the button, this means that you are getting the count on the post back, and not on the original display of the page. But you populate the gridview on the original display. So at the time of the postback, you no longer have the dataset available, so you can't just say textbox1.text=ds.tables(0).rows.count. You would have to either (a) rerun the query, which seems like a waste of resources, or (b) stash the count somewhere on the initial load.
I'd suggest (b). You could populate the text box on the initial load and hide it (textbox1.visible=false). Or you could create a hidden field to keep the count, or put it in the view state, and then retrieve it from there when you need it.
Two by the ways:
One: I'd use meaningful names for controls rather than "textbox1" and "gridview1". It's a lot easier to read a program that says
if update_order.checked then ...
rather than
if checkbox1.checked then ...
Two: You have some unnecessary steps in your database call. You don't need the xp.ExecuteNonQuery. The fill() will execute the query. You're running it twice: the first time you throw away the results, and then the second time you process them. Also, you don't normally need to specify the data type on your parameters. You can just write
xp.parameters.AddWithValue("@search",textbox1.text)
Upvotes: 0
Reputation: 313
I am on Linux now and I am coding in RoR.... I cannot test now... But if you need to know how many items has your GridView, just use:
int qty = GridView1.Rows.Count;
Is it what you need?
Some samples... always find in MSDN abour Microsoft Developer info, There are a lot of stuff there!
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rows%28v=vs.110%29.aspx
Upvotes: 1
Reputation: 3182
Amount of rows that has been returned will store in instance of dataset. so by taking the count you will get the returned row information.The DataSet contains rows, columns,primary keys, constraints, and relations with other DataTable objects. We can get the number of rows from in a Table inside a Dataset by using its Rows.Count property.
ds.Tables[0].Rows.Count
Upvotes: 1
Reputation: 386
To get the total rows
int TotalRecord = dt.Rows.Count();
You can probably just set everything in your OnClick event but if it doesn't work you may have to set your row count when the gridview is being created and then I would create a label and set the label = TotalRecord.text
. make the label.visible=False
and in your buttons onclick event set the label.visible = true
.
Upvotes: 0