I am returning SQL results. First I guess should I be using the reader or the adapter? Second I want to output the results to a table (I cant use a GridView because of other functions going on in the page). So I am wondering How would I go about building my own table and populating with Data from mySQL database. I cant find anything that tells me how to output to what I want. I find textboxes, labels, files etc. but not inline html? I am using ASP.net and C#
Thanks
Reader will be faster than adapter, you will get better performance and your life will get more complicated once you start creating your own grid using html table.
If you don't want to write a lot of code, then use adapter as you will get DataTable which you can easily loop through and build your own html table.
What you are looking for is: System.Web.UI.HtmlControls namespace.
HtmlTable
HtmlTableCell
HtmlTableCellCollection
HtmlTableRow
HtmlTableRowCollection
You can build HtmlTable and render it to page. Take a look at this example: Creating HTML Table...
Also, you may want to look at HtmlGenericControl where you can create any element you want like: new HtmlGenericControl("div");
Another option is to use a repeater control for emitting the data. Clean, easy, and you have full control of the markup generated.
Here's an example: http://www.aspcode.net/ASPNET-Repeater-control-instead-of-grid.aspx
Related
Below is my 46 asp.net control including textboxes and dropdownlist to insert data in single button
Below is my asp.net control id as above image given image
ddl_tag1 txt_date1 ddl_type1 txt_narration1 txt_amount1 txt_bill1 txt_fromloc1 txt_tolocation1
ddl_tag2 txt_date2 ddl_type2 txt_narration2 txt_amount2 txt_bill2 txt_fromloc2 txt_tolocation2
ddl_tag3 txt_date3 ddl_type3 txt_narration3 txt_amount3 txt_bill3 txt_fromloc3 txt_tolocation3
ddl_tag4 txt_date4 ddl_type4 txt_narration4 txt_amount4 txt_bill4 txt_fromloc4 txt_tolocation4
ddl_tag5 txt_date5 ddl_type5 txt_narration5 txt_amount5 txt_bill5 txt_fromloc5 txt_tolocation5
ddl_tag6 txt_date6 ddl_type6 txt_narration6 txt_amount6 txt_bill6 txt_fromloc6 txt_tolocation6
below is my table structure the above six row data insert one by one into below format
Sno TAQ DATE TYPE NARRATION AMOUNT BILL FROM LOCATIOM TO LOCATION
What is the best way to insert data using add button, i have to use for each loop for insertion for above text boxes and drop-down to insertion one row then another row insertion.
If you are manually putting a bunch of controls onto a page, and not databinding them to anything, creating six manual insert statements is pretty much the only option as far as I know.
As one commenter said above, a gridview might be easier, but honestly if the point is solely just to add and not to also display/update (which is what gridviews are best at) I'm not sure how much that would gain you. If the requirement is specifically and only to add six new rows at a time, you're probably doing the easiest thing.
If you ultimately want more functionality than just add records, you may want to take a look at gridview though.
I have googled and seems not to find a perfect answer. Most people make use of a gridview. This is what i'm trying to achieve. I am developing a website. I have about 50 records in my database. I want to display 7 records per paga and then provide a next and back button. i dont want to make use of a gridview because that wont be okay. I want to proper pagination technique where each data will be displayed in a div. I dont known what is used for pagination is a site like this but i want to handle my pagination like this too. Will really appreciate your suggestions and codes. A demo would be really appreciated. Help a rooky please. Please note that i don't want to use tables. Want something like that of php
You can use a Repeater control, and assign to it the data source.
After loading data you get the total count, and divide by 7 to get page count, then create each link that point to the same page with > .aspx?page=X .
On load you use the paging technique in your DB engine to get the proper page and assign to repeater as data source.
Do yourself a favor and look into jQuery Datatables. You can still use a gridview, listview, table, whatever and Datatables sits on top of it. Handles paging, sorting, filtering, search automatically. And on top of that it's free.
The learning curve isn't that steep either, you basically download the scripts, refer to them in your code, and then apply the jquery to your table/grid object.
https://datatables.net/
Following on from the comments you would want to do something like this to get the data
SELECT *
FROM YOURTABLE
ORDER BY SOMECOLUMN
OFFSET x ROWS
FETCH NEXT x ROWS ONLY
-- x represents the number of rows you want to bring back
-- I would recommend you keep that the same as each other
then you would want something in your query string
http://yoursite?Offset=10&Fetch=10
this would be whats passed back to your code behind.
Now depending on your level of competence with .Net you can get the query string in a few ways
var fetch = 0;
var offset = 0
if (string.IsNullOrEmpty(Request.QueryString["Fetch"].ToString()))
{
fetch = Convert.ToInt32(Request.QueryString["Fetch"]);
}
if (string.IsNullOrEmpty(Request.QueryString["Offset"].ToString()))
{
offset = Convert.ToInt32(Request.QueryString["Offset"]);
}
The pass these values into a stored procedure. I am going to assume you know how do this.
Once you have the data back from the stored procedure and have the details in a DataTable unless youre using EF.
Loop through the you can either use a Repeater control as suggested in an earlier answer.
then you can pass the details back to the screen and this is where things will get interesting as I cannot assume that your using MVC or WebForms. So the rest of this journey you're on your own I'm afraid.
From time to time, I need to create an input control which allows multiple rows of input, the type of input and number of columns could be different in each case but typically it would be represent a database table being edited, so it should also be possible to store an id value within each row. It should also be possible for the user to add new rows to the input if they have more data to input.
I have tried a couple of approaches to this but they are all very long winded for what seems like such an obvious and common scenario. I'm thinking that there must be a simple way to do this that I have missed.
The way that I usually solve this is by using a list view, enter all of the input controls required within the item template and use a html table for formatting, with each item being a row of the table. If there is existing data to be edited, I would load the data from the database, add a blank object to the results and bind it to the list view. If there is no existing data, I would create a collection with a blank record in it and bind it to the list view. I add a button for adding a new row. When clicked, this button retrieves all of the existing data from the list view by iterating all of the listview items and populating the data into a collection, which I would then add a blank object to and rebind the listview with the results. When saving, I would retrieve the results by iterating the listview items again and using a hidden field within each row to store the record id. This would determine whether a new record was added or an existing record was edited.
This approach works for me but I'm sure there must be simpler ways to achieve this. Looking forward to seeing how other people solve this.
For web forms, use a GridView. You can set its properties to allow editing, deleting, new rows, sorting, and paging.
Check out the MSDN example here that shows how to use it. If you bind a datasource to it, it will figure out the columns and adjust dynamically then, or you can predefine the columns you want for more customability.
You are talking about bulk insert/update. You could use XML insertion/updation for this purpose. Get all the data to a DataSet ds variable. Then use ds.GetXml() method to convert the dataset to XML string. Pass this to an XML parameter into SQL server which has the datatype 'XML'
INSERT INTO YOURTABLE
SELECT
V.VOI.value('(.)[1]', 'int')
FROM
#Input.nodes('/DATASETNAME/DATATABLENAME/') V(VOI)
Use this link to learn more
You need dynamic table with Add, View,Edit and delete operations on each data.
I would suggest using DataTable Jquery component. there are tons of examples on Data operations, and you can plug this with any Server technology including ASP.net
http://editor.datatables.net/
I've been trying to find a way to remove all the empty rows and columns from my htmlTable. Is there a way to convert the htmlTable into a DataSet, remove the emptys rows and columns and then convert it back to a htmlTable? Can this be accomplished without converting it to a DataSet? I'm pretty sure I'll have to iterate through the entire table and the rows/columns, but beyond that I'm lost. I will still need the final output to remain as a htmlTable as this information is being displayed within a asp form.
Any help is greatly appreciated.
Why do you have empty rows? Are you sending those empty items to the client? Is a user deleting specific rows or something? I'd like to see more info about your set up but, to me, it seems pointless to do this server side. Unless it's possible to not send those empty items to the client I'd use jQuery to do the removal client side. See Using JQuery to remove blank rows from HTML Tables about doing this with javascript / jQuery.
I am working on a ASP.NET/C# web application.
I am writing a Jquery script.
I would like to know what is the best way to add data to the script from an SQL database .
Here is a quick example showing what I would like to do:
Suppose I have a database like this
Name Color
------------+---------------+
Tom FF0000
Kate 00FF00
John 0000FF
In my script I want to do this:
$(function(){
$('#main').Picture({
colors: {Tom:'#FF0000', Kate:'#00FF00', John:'#0000FF'}
});
});
the colors should be red from the database and not hard-coded.
What is the best way to insert the colors in the correct place in my code? Should I do it in a literal control and inject the script from the code behind?
Another example:
$('#location').html('Here we put the text that I got from the database');
Thank you for any help
Suggested Scenario: Get you data from your SQL database normally like you used to, then fill the data into any web control of your choice (I prefer to use a HiddenField in this case) then the Hidden field will be available to your Javascript/jQuery code.
Example:
here are the steps you need to follow in order to get this problem fixed:
1- Create an asp:HiddenField control in your page with a proper ID (ex: 'hfMyData').
2- In your c# code, get your data from your database:
var mydata = // code to get the data from your Database
3- Fill the HiddenField value with your data:
hfMyData.value = mydata;
4- In your jQuery code Ready() function, use code similar to this:
var myDatabaseData = $("#hfMyData").val();
5- Now you have the data from your Database (myDatabaseData), use it as you want.
What I've done in the past (although it's a bit quick and dirty), is create a public property on the page, then call it in the jQuery using the <% %> tags. That way you can set the property in the code behind via the DB call, then pull them through as required.
Not very scalable, but worked well for the scenario I required (needed a twitter handle).