001.
<%@ Page Language=
"C#"
Debug=
"true"
%>
002.
<%@ import Namespace=
"System.Data"
%>
003.
<%@ import Namespace=
"System.Data.Odbc"
%>
004.
<script runat=
"server"
>
005.
OdbcConnection objConn;
006.
OdbcCommand objCmd;
007.
String strSQL;
008.
009.
void
Page_Load(Object sender, EventArgs e)
010.
{
011.
String strConnString;
012.
strConnString =
"Driver={SQL Server};Server=localhost;Database=mydatabase;UID=sa;PWD=;"
;
013.
objConn =
new
OdbcConnection(strConnString);
014.
objConn.Open();
015.
016.
if
(!Page.IsPostBack)
017.
{
018.
BindData();
019.
}
020.
}
021.
022.
void
BindData()
023.
{
024.
strSQL =
"SELECT * FROM customer"
;
025.
026.
OdbcDataReader dtReader;
027.
objCmd =
new
OdbcCommand(strSQL, objConn);
028.
dtReader = objCmd.ExecuteReader();
029.
030.
031.
myGridView.DataSource = dtReader;
032.
myGridView.DataBind();
033.
034.
dtReader.Close();
035.
dtReader =
null
;
036.
037.
}
038.
039.
void
Page_UnLoad()
040.
{
041.
objConn.Close();
042.
objConn =
null
;
043.
}
044.
045.
void
modEditCommand(Object sender , GridViewEditEventArgs e)
046.
{
047.
myGridView.EditIndex = e.NewEditIndex;
048.
myGridView.ShowFooter =
false
;
049.
BindData();
050.
}
051.
052.
void
modCancelCommand(Object sender, GridViewCancelEditEventArgs e)
053.
{
054.
myGridView.EditIndex = -1;
055.
myGridView.ShowFooter =
true
;
056.
BindData();
057.
}
058.
059.
void
modDeleteCommand(Object sender, GridViewDeleteEventArgs e)
060.
{
061.
strSQL =
"DELETE FROM customer WHERE CustomerID = '"
+ myGridView.DataKeys[e.RowIndex].Value +
"'"
;
062.
objCmd =
new
OdbcCommand(strSQL, objConn);
063.
objCmd.ExecuteNonQuery();
064.
065.
myGridView.EditIndex = -1;
066.
BindData();
067.
}
068.
069.
070.
void
myGridView_RowCommand(Object source , GridViewCommandEventArgs e)
071.
{
072.
if
(e.CommandName ==
"Add"
)
073.
{
074.
075.
TextBox txtCustomerID = (TextBox)myGridView.FooterRow.FindControl(
"txtAddCustomerID"
);
076.
077.
TextBox txtName = (TextBox)myGridView.FooterRow.FindControl(
"txtAddName"
);
078.
079.
TextBox txtEmail = (TextBox)myGridView.FooterRow.FindControl(
"txtAddEmail"
);
080.
081.
TextBox txtCountryCode = (TextBox)myGridView.FooterRow.FindControl(
"txtAddCountryCode"
);
082.
083.
TextBox txtBudget = (TextBox)myGridView.FooterRow.FindControl(
"txtAddBudget"
);
084.
085.
TextBox txtUsed = (TextBox)myGridView.FooterRow.FindControl(
"txtAddUsed"
);
086.
087.
strSQL =
"INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
+
088.
" VALUES ('"
+ txtCustomerID.Text +
"','"
+ txtName.Text +
"','"
+ txtEmail.Text +
"' "
+
089.
" ,'"
+ txtCountryCode.Text +
"','"
+ txtBudget.Text +
"','"
+ txtUsed.Text +
"') "
;
090.
objCmd =
new
OdbcCommand(strSQL, objConn);
091.
objCmd.ExecuteNonQuery();
092.
093.
BindData();
094.
}
095.
}
096.
097.
098.
void
modUpdateCommand(Object sender,GridViewUpdateEventArgs e)
099.
{
100.
101.
TextBox txtCustomerID = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditCustomerID"
);
102.
103.
TextBox txtName = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditName"
);
104.
105.
TextBox txtEmail = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditEmail"
);
106.
107.
TextBox txtCountryCode = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditCountryCode"
);
108.
109.
TextBox txtBudget = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditBudget"
);
110.
111.
TextBox txtUsed = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditUsed"
);
112.
113.
strSQL =
"UPDATE customer SET CustomerID = '"
+ txtCustomerID.Text +
"' "
+
114.
" ,Name = '"
+ txtName.Text +
"' "
+
115.
" ,Email = '"
+ txtEmail.Text +
"' "
+
116.
" ,CountryCode = '"
+ txtCountryCode.Text +
"' "
+
117.
" ,Budget = '"
+ txtBudget.Text +
"' "
+
118.
" ,Used = '"
+ txtUsed.Text +
"' "
+
119.
" WHERE CustomerID = '"
+ myGridView.DataKeys[e.RowIndex].Value +
"'"
;
120.
objCmd =
new
OdbcCommand(strSQL, objConn);
121.
objCmd.ExecuteNonQuery();
122.
123.
myGridView.EditIndex = -1;
124.
myGridView.ShowFooter =
true
;
125.
BindData();
126.
}
127.
128.
</script>
129.
<html>
130.
<head>
131.
<title>ThaiCreate.Com ASP.NET - System.Data.Odbc</title>
132.
</head>
133.
<body>
134.
<form id=
"form1"
runat=
"server"
>
135.
<asp:GridView id=
"myGridView"
runat=
"server"
AutoGenerateColumns=
"False"
136.
ShowFooter=
"True"
137.
DataKeyNames=
"CustomerID"
138.
OnRowEditing=
"modEditCommand"
139.
OnRowCancelingEdit=
"modCancelCommand"
140.
OnRowDeleting=
"modDeleteCommand"
141.
OnRowUpdating=
"modUpdateCommand"
142.
OnRowCommand=
"myGridView_RowCommand"
>
143.
144.
<Columns>
145.
146.
<asp:TemplateField HeaderText=
"CustomerID"
>
147.
<ItemTemplate>
148.
<asp:Label id=
"lblCustomerID"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'
></asp:Label>
149.
</ItemTemplate>
150.
<EditItemTemplate>
151.
<asp:TextBox id=
"txtEditCustomerID"
size=
"5"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'
></asp:TextBox>
152.
</EditItemTemplate>
153.
<FooterTemplate>
154.
<asp:TextBox id=
"txtAddCustomerID"
size=
"5"
runat=
"server"
></asp:TextBox>
155.
</FooterTemplate>
156.
</asp:TemplateField>
157.
158.
<asp:TemplateField HeaderText=
"Name"
>
159.
<ItemTemplate>
160.
<asp:Label id=
"lblName"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Name") %>'
></asp:Label>
161.
</ItemTemplate>
162.
<EditItemTemplate>
163.
<asp:TextBox id=
"txtEditName"
size=
"10"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Name") %>'
></asp:TextBox>
164.
</EditItemTemplate>
165.
<FooterTemplate>
166.
<asp:TextBox id=
"txtAddName"
size=
"10"
runat=
"server"
></asp:TextBox>
167.
</FooterTemplate>
168.
</asp:TemplateField>
169.
170.
<asp:TemplateField HeaderText=
"Email"
>
171.
<ItemTemplate>
172.
<asp:Label id=
"lblEmail"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Email") %>'
></asp:Label>
173.
</ItemTemplate>
174.
<EditItemTemplate>
175.
<asp:TextBox id=
"txtEditEmail"
size=
"20"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Email") %>'
></asp:TextBox>
176.
</EditItemTemplate>
177.
<FooterTemplate>
178.
<asp:TextBox id=
"txtAddEmail"
size=
"20"
runat=
"server"
></asp:TextBox>
179.
</FooterTemplate>
180.
</asp:TemplateField>
181.
182.
<asp:TemplateField HeaderText=
"CountryCode"
>
183.
<ItemTemplate>
184.
<asp:Label id=
"lblCountryCode"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'
></asp:Label>
185.
</ItemTemplate>
186.
<EditItemTemplate>
187.
<asp:TextBox id=
"txtEditCountryCode"
size=
"2"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'
></asp:TextBox>
188.
</EditItemTemplate>
189.
<FooterTemplate>
190.
<asp:TextBox id=
"txtAddCountryCode"
size=
"2"
runat=
"server"
></asp:TextBox>
191.
</FooterTemplate>
192.
</asp:TemplateField>
193.
194.
<asp:TemplateField HeaderText=
"Budget"
>
195.
<ItemTemplate>
196.
<asp:Label id=
"lblBudget"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Budget") %>'
></asp:Label>
197.
</ItemTemplate>
198.
<EditItemTemplate>
199.
<asp:TextBox id=
"txtEditBudget"
size=
"6"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Budget") %>'
></asp:TextBox>
200.
</EditItemTemplate>
201.
<FooterTemplate>
202.
<asp:TextBox id=
"txtAddBudget"
size=
"6"
runat=
"server"
></asp:TextBox>
203.
</FooterTemplate>
204.
</asp:TemplateField>
205.
206.
<asp:TemplateField HeaderText=
"Used"
>
207.
<ItemTemplate>
208.
<asp:Label id=
"lblUsed"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Used") %>'
></asp:Label>
209.
</ItemTemplate>
210.
<EditItemTemplate>
211.
<asp:TextBox id=
"txtEditUsed"
size=
"6"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Used") %>'
></asp:TextBox>
212.
</EditItemTemplate>
213.
<FooterTemplate>
214.
<asp:TextBox id=
"txtAddUsed"
size=
"6"
runat=
"server"
></asp:TextBox>
215.
<asp:Button id=
"btnAdd"
runat=
"server"
Text=
"Add"
CommandName=
"Add"
></asp:Button>
216.
</FooterTemplate>
217.
</asp:TemplateField>
218.
219.
<asp:CommandField ShowEditButton=
"True"
CancelText=
"Cancel"
DeleteText=
"Delete"
EditText=
"Edit"
UpdateText=
"Update"
HeaderText=
"Modify"
/>
220.
<asp:CommandField ShowDeleteButton=
"True"
HeaderText=
"Delete"
/>
221.
222.
</Columns>
223.
</asp:GridView>
224.
</form>
225.
</body>
226.
</html>