.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

GridView - table v/s SqlDataSource ?

Posted By:      Posted Date: October 25, 2010    Points: 0   Category :ASP.Net

Hi all,

I have a gridview which i need to populate from a products database, where a user can click on a certain

product (row) and gets directed to a new page with that product ID (where he will see the product details

and so on..)

while this seems easy if i bind the gridview to a SQL Datasource, i face few problems:

1- I need to trim the product descrption and only display the first 30 characters ; some products descrption

is much more than 30 characters and i dont want to display all of it in the gridview ..

2- there is a date/time field in my sql query, which appears in the gridview as "10/21/2010 12:00:00 AM". I

don't want to display the time stamp, but only the date. any idea how?

3- if there are no data to display , the gridview header (product name, product description, entry date,

etc..) will disappear and thus the user will be confused..

As a workaround, I'm thinking of capturing the date in a Datatable, after having manipulated it as required

above, and them running a databind on the gridview, which is feasible but has its own challenges:

1- how do u display a URL with the product ID as a variable?

I'm guessing the first method is better, but I couldn't figure it out...

View Complete Post

More Related Resource Links

Binding GridView with SqlDataSource at runtime?


Hi, I want to create a generic page which contains a gridview and sqldatasource. I want to put these two controls on .aspx (C#) page but bind them at runtime. It should have selectcommand, updatecommand, deletecommand and insertcommand. Stored procedures are created for that. Help is needed to make it. Please post some link or code. Regards, ap.

Gridview Update outside of SqlDatasource


Is it possible to use a SQLDataSource to populate a gridview but not use the SQLDataSource for Updating?

I wanted to add the update code in the Gridview_RowUpdating event since it is a complex update I need to do.

I get this message when i click the update button

Updating is not supported by data source 'SqlDataSource1' unless UpdateCommand is specified.

Return value from SqlDataSource in GridView ?

Hi, everyone. I have textbox for searching a Names in My database. I return the information from the Select Statement in  GridView, but i want to know the number of affecter rows. How to do that?

gridview table alignment problem

hi all, i designed a gridiview, within that ItemTemplate i drew an table then tr then some of td's, now the problem is after every row im getting one empty row, what is the problem and how to resolve it. below is my code <ItemTemplate>                                             <table cellspacing="0" cellpadding="0" style="border-right:1px solid Black;" >                                                 <tr  >                                                     <td width="1%" align="center" style="border-right:1px solid Black;">          &n

Textbox -> value -> gridview -> Sqldatasource?

Hi guys. I have textbox that searches for first or last name. Result show in gridview. But when add value in text box and push my button Find nothing happen. Here code: aspx:   ........ <asp:GridView runat="server" AllowPaging="True"                   AutoGenerateColumns="False" DataKeyNames="MobileNum"                   DataSourceID="SqlDataSource5" style="font-family: Verdana"                          onselectedindexchanged="GridView1_SelectedIndexChanged">         <Columns>                         <asp:BoundField DataField="FNAME" HeaderText="FNAME" SortExpression="FNAME" />             <asp:BoundField DataField="LNAME" HeaderText="LNAME" SortExpression="LNAME" />          &nb

hoq to fill gridview dropdownlist from another db table

Hi, I have a gridview which gets its data from a database table now i ghave added a dropdownlist to this gridview but this dropdown should get the data from another table and should set its value automatically with a foreign key from the  gridview i have added the code like below in my rowDatabound but it just loads 1 field and then how should i get the selectedValue according to the gridview id field?? Protected Sub Friends_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)             If e.Row.RowType = DataControlRowType.DataRow Then                 Dim conn As New SqlConnection(HttpRuntime.Cache("con"))                Dim sql As String = "select ID,ListName from FriendList order by ListName asc"                Dim cmd As New SqlCommand(sql, conn)                cmd.CommandType = CommandType.Text                conn.Open()             &

GridView bound to SqlDataSource = 2 tier ?


Hi guys,

I've got two questions about n-tier architecture.

I have an aspx page with 2 controls on it - a GridView bound to a SqlDataSource control.
Am I correct in thinking this is 2-tier ?

If I were to replace the SqlDataSource with an ObjectDataSource, and make the database connections/calls/queries via a class in the App_Code folder, this would be 3-tier, right ?

SQLdatasource + gridview + mysql




I have a sqldatasource and a gridview. I work with a mysql 3.51 database.

My problem is :

When I try tu update a record through the update command of the gridview, it doesn't work.  Here is my code :


asp:SqlDataSource ID="SQL_Panier" runat=server ConnectionString="<%$ ConnectionStrings:Connection to NI %>" ProviderName="<%$ ConnectionStrings:Connection to NI.ProviderName %>" SelectCommand="selectcommand"

UpdateCommand="Update Panier SET qtt=@qtt,commentaires=@commentaires WHERE email=?"

pull some data from a table twice for same gridview


 Hello all - I have been searching google for a solution to this problem but I'm having difficulty describing it in keywords.

environment:  VWD 2010 Express, VB, SQL Express, .NET 4.0

I have a gridview that currently pulls data from a table called CrossRef which has the ID numbers of tasks that need to go in a certain sequence.  So the gridview looks like:

Task 1       Sequence          Task 2           XRefID

10               BEFORE             22                 4

2                AFTER                6                  5

17              BEFORE              2        

Gridview Sorting with SqlDataSource



I had a previous post (found here for reference: http://forums.asp.net/p/1573740/3954537.aspx#3954537) Where I was wanting to hide/assign gridviews and datasource dynamically to reduce the page load time. I now have a user requesting that one of those Gridviews be sortable. I have already gone through and added the sort Expression to each column that is created in the code behind. 

I also added a Gridview Sorting Handler

Protected Sub billingGridview_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles billingGridView.Sorting

But, when I rebind the data, how to I fill in the sort direction and sort Field? The datasource gets its information from a Stored Proc that does not have an Order By (parameter) in it. The websites I have been reading say to use a datatable or dataview and send the sort field and direction to the query. If I dont do all this in the code behind and allow sorting, the gridview is able to handle sorting itself. Is there any way to do this from the code behind? Or am I going to have to rework this with a query that contains a parameter for the sort direction and field?



gridview.rowcommand + sqldatasource.delete()


I have gridview+sqldatasource. In gridview i have button DELETE. But i want on click this button for some rows execute DELETE command other rows UPDATE. I did:

protected void GridMain_RowDataBound(object sender, GridViewRowEventArgs e)
  if (e.Row.RowType == DataControlRowType.DataRow)
     if (DataBinder.Eval(e.Row.DataItem, "DelFlag").ToString() == "True")
               btnDelete.CommandName = "Delete";
               btnDelete.CommandName = "DeleteVirtual";

protected void GridMain_RowCommand(object sender, GridViewCommandEventArgs e)
        if (e.CommandName == "Delete")
           sourceMain.DeleteCommand = "DELETE FROM [Main] WHERE 
        else if (e.CommandName == "DeleteVirtual")
            sourceMain.DeleteCommand = "UPDATE [Main] SET DelFlag = 'True' WHERE  
@index - DeleteParametrs in sqldatasource. But DELETE works and DELETEVIRTUAL doesn't. Then i insert sourceMain.Delete() in rowcommand after sourceMain.DeleteCommand ... But it's doesn't help m

how to One GridView (table ) Copy to other page Gridview (table)


how to One GridView (table ) Copy to other page  Gridview (table)

How to INSERT GridView data into another table (besides the one it is bound to)??


Hi All,

I have a tricky situation that I just cannot seem to figure out.  I'm using ASP.NET 4.0.  I have two databases (let's call them Northwind and MyDatabase for simplicity).  I cannot modify the data in Northwind, but once I save a copy to MyDatabase, I can change it.

I have a GridView that shows all the tests associated with a patient.  I first populate the GridView, retrieving all the tests in Northwind.Test that are associated to a PatientID.  On a button click event, I need to:

  1. Create a new Patient record in MyDatabase.Patient (I've got this part done already).
  2. Use that newly created PatientID and INSERT all of the rows from the GridView into MyDatabase.Test.  I have the PatientID available, I just don't know how to retrieve the data from the GridView.
  3. (Ideally) I would like to then be able to load that patient record in the future from MyDatabase and be able to UPDATE the tests in the GridView to MyDatabase.Test.

I can load the tests into the GridView no problem.  But I have no idea how I would save that data to a different datasource.  Any ideas?  Should I even be using a GridView, or something else?

-Thanks in advance-

get a correct row index after refresh the gridview table


hi all,

i am facing a problem to get a correct row index after i filter the data in table, it always keep the history row index in the table  before i filter. i had a search button to filter the data in gridview,

below is my coding:


 <asp:Button ID="btn_reissue" runat="server" Text="Reissue" CommandName="reissue"  CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>' OnClientClick="return window.confirm('Are you sure you want to reissue this record?');" Width="65px" />


    Protected Sub grdview_rowcommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridView1.RowCommand
        Dim cmd As SqlCommand
        If e.CommandName = "reissue" Then
            If Not pUser Is Nothing Then
                pDbUserCon = New SqlConnection(pDbESS)
                pDbUserTrx = pDbUserCon.BeginTransaction()
                    Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                    Dim row As GridViewRow

Breaking long query into small one using SQLDataSource (using Temp table)


Currently, I have long script which does major processing and then puts data into temp table.

Finally I read data from this temp table to show on the chart, based on user's selection of parameter. So I can divide my query into two parts and while I am researching this, I will appreciate any pointers/guidance.


Is it possible to do processing in one Datasource and read table from second datasource?

Is it even possible?

If not, what is recommended method for this (breaking large processing into small one)?

Gridview Header & table data alignment problem



Newbie to ASP.net. Programming in C#.

I'm querying a SQL database and displaying the results in a gridview.

The results can be many records so I need to provide the facility to scroll. To display the headers when scroling I have implemented ( in my asp code) the headers in another table and the data is in another table. I see now I cannot align the column headers with the table data.

Appreciate any help to fix this issue.

If this method is not a good one to display headers with data please suggest another method.( Some example code guidance would really help)

Here is my ASP code.

<div id="divProjectTableHeaderFullDetail" runat="server" style="background-color: #993300;
            height: 40px; width: 770px; margin: 0;  padding: 0 ; " >
            <table style="font-family: Arial; font-size: 10pt; color: white;" cellspacing="0"

using data in a gridview to update a table



I have a gridview that gathers data from two tables, an orders table and a products table. I have an OrderQuantity from the Orders table and a StockQuantity from the Products table and a button to process the order.

I am trying to move through the GridView one line at the time, substract the OrderQuantity from the StockQuantity and update the Product table.

How do I move through the GridView?



ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend