I just know how to populate gridview with asp:SqlDataSource
But I have a column of TemplateField
in my gridview
, when I need mo
A template field
in gridview
is a field containing one of the values which you selected from database in your data-source.
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'>
</asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("LastName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
There template for Item as well edit item.
Item template is shown when gridview is not in edit mode.
And edit item template is shown when it is in the edit mode.
More Details:- http://msdn.microsoft.com/en-us/library/bb288031.aspx
I just created a basic page with a SqlDataSource that does all that you are wanting to do.
Here's what I did. Drag a SqlDataSource from the Toolbox over onto your page in Design view. Click the Smart Tag and Configure Data Source. Click New Connection. If you are working with a local installation of MySql then most likely the Server Name is localhost, the user name is root, and the password is whatever you specified when you installed and configured MySql server on your computer. Pick your database. Always a good idea to click the Test Connection button here to make sure it's working. Now, walk through the rest of the wizard (Next, Next). When you get to the "Configure the Select Statement" page, after you have specified which table you are querying and which fields, make sure that you click on the advanced button on the right and click the Generate Insert, Update, and Delete statements. Now finish out the wizard.
At this point, you need to go to source view. For me, the statements that are generated are not correct MySql syntax, so you have to fix them. Easiest is to do a find/replace in source view and replace all "["'s and "]"'s with "`".. Keep in mind, that's not an apostrophe. It's the other similar looking character (I don't know what you call it) to the left of your 1 on the top number row of your keyboard. You will also need to add the actual field names after the "?"'s in the insert, update and delete statements in order for the parameters to work correctly.
Ok, now go back to your Design View, drag a GridView control onto your page, in the Smart Tag for the GridView, select Choose Data Source and select the data source on your page which you just created (probably called SqlDataSource1 unless you changed the name when you created it). Hit refresh schema. If it's working, it will might give you some confirmation box, but afterwards, it should refresh your GridView and show the columns in your data source. Click your smart tag in your GridView again and click checkboxes next to Enable Paging, Sorting, Editing, Deleting (whatever you want). Now save the page and run it. You should see a GridView with ability to do all those things.
For inserting, you need a different control. A DetailsView control is probably your best bet. The steps are almost identical to what you did with the GridView.
Here's the source code I have after doing all that:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString2 %>"
DeleteCommand="DELETE FROM `test` WHERE `TestID` = ?TestID"
InsertCommand="INSERT INTO `test` (`TestID`, `FirstName`, `LastName`) VALUES (?TestID, ?FirstName, ?LastName)"
ProviderName="<%$ ConnectionStrings:testConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM `test`"
UpdateCommand="UPDATE `test` SET `FirstName` = ?FirstName, `LastName` = ?LastName WHERE `TestID` = ?TestID">
<DeleteParameters>
<asp:Parameter Name="TestID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="TestID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="TestID" Type="Int32" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
DataKeyNames="TestID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="TestID" HeaderText="TestID" ReadOnly="True" SortExpression="TestID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
</Columns>
</asp:GridView>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="TestID"
DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
</form>
</body>
</html>
All this assumes you followed the steps above which will create a connection string in your web.config file for you. So you won't be able to copy this code verbatum and have it work. Not without the connection string in your web.config anyway.
There's a lot more you could do. Lots of great articles and videos on this site about using the different data controls. There are also some things to consider when deploying to production. In case it helps, I use GoDaddy and wrote an article last month about setting up ASP.NET Membership using MySql and hosting on GoDaddy. I think the first part of that article might give you some additonal helpful hints depending on what you are ultimately trying to do.
That article is here:
http://www.marvinpalmer.com/MarvinPalmer/post/Implement-NET-Membership-and-Roles-using-MySql-Connector-523-on-GoDaddy.aspx
check dis link u can definetly solve the problem........
http://aspnet.4guysfromrolla.com/articles/021203-1.aspx [with out paging]
http://www.aspnetdatagrid.com/Ex/RowInUpDl.aspx [with paging]
Hope this helps.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.Common;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;
public partial class viewAdmin : System.Web.UI.Page
{
String MyConString = "SERVER=localhost;" +
"DATABASE=databasename;" +
"UID=root;" +
"PASSWORD=;";
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection(MyConString);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM tablename;", conn);
conn.Open();
DataTable dataTable = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dataTable);
GridVIew.DataSource = dataTable;
GridVIew.DataBind();
}
}