|
下面给出了如何在我的数据库文件代码中增加totaldownloads值- l3 I5 c/ S. `
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());' W) s2 |0 a) i; t% {. z8 g
SqlCommand sqlcmd = new SqlCommand();
' S7 @" I- j3 U SqlDataAdapter da = new SqlDataAdapter();0 [3 A3 R3 |/ Q8 I( {$ x% J, O
DataTable dt = new DataTable();
$ N5 B2 j( @* J; h) k/ r( s) R) p DataRow dr;# ]2 B6 C* C( D2 o1 x' R
protected void Page_Load(object sender, EventArgs e)
: f/ h. [, b0 O1 E% E {3 k; u2 X' }! i3 c, y
if (Session["UserId"] == null)
) l# t& n" l" t& g! m. n {
1 P( K" p* s f6 @9 h G. k lblMessage.Visible = true;
9 {. x k9 a7 o GridView1.Visible = false;
. L% {% D, @- S$ q7 q { //AppContent.Visible = false;( o3 R% Y4 W0 G- m8 V4 r6 o" F3 G$ o
}
# o8 W L- n1 v else8 G# [0 s9 [+ B5 @# y
{; O( M: b. Q8 D1 M7 ?. I0 T$ ?
if (!Page.IsPostBack)
4 @0 C; F0 ^" X: I0 Z, n b5 m {2 z5 J0 ^3 ]3 G$ `; E! v
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());# H0 O- h$ W, }1 i8 K$ ?
ArrayList myArrayList = ConvertDataSetToArrayList();
% b. w! c4 D# f8 X' A, z Literal objliteral = new Literal();
5 S8 g9 D" {* |) Z3 _ StringBuilder objSBuilder = new StringBuilder();
3 b5 K2 ^* w4 [7 d4 [6 w, }; u' G //Add some column to datatable display some products 3 S/ N1 G8 r$ E( s$ Y5 c
dt.Columns.Add("appImg");
( B4 f( ]! k p% Z/ a9 R3 k dt.Columns.Add("appName");. z0 a; \8 D2 l+ ~4 B( Z
dt.Columns.Add("appLink");
0 r7 u( |, e8 g% k& Z dt.Columns.Add("appType");
: U' N) M4 X$ a9 \$ [1 Q t6 k dt.Columns.Add("TotalVisitors");
0 `. o2 d+ W, m dt.Columns.Add("TotalDownloads");
. L' P" ]) z9 A, f: u; V dt.Columns.Add("RemainingVisitors");
% @0 N6 R, F* _: \, J- O5 \: |, K1 @ // Display each item of ArrayList
1 ]$ f( j6 r4 Y+ N) I4 c+ Z3 I foreach (Object row in myArrayList)# }* Z. O9 _+ z: b( T# ]
{
+ n" [# C3 ]. _# X* D- x //Add rows with datatable and bind in the grid view4 i# v- K6 \4 Q
dr = dt.NewRow();
2 x1 d* |3 W! s& a$ X0 N dr["appImg"] = ((DataRow)row)["AppImg"].ToString();% l$ z$ H6 a0 C7 \2 w
dr["appName"] = ((DataRow)row)["AppName"].ToString();
- w4 L4 w. o) ~" { dr["appLink"] = ((DataRow)row)["AppLink"].ToString();
3 Q( l) O" r( t, z5 i- p dr["appType"] = ((DataRow)row)["AppType"].ToString();3 ~6 P- Y, B/ m' h; e9 m' ^% l3 b
dr["TotalVisitors"] = "lan Visitors: " + ((DataRow)row)["TotalVisitors"].ToString();
& q* H, P6 P: R' T+ S; C dr["TotalDownloads"] = "Downloaded: " + ((DataRow)row)["TotalDownloads"].ToString();
; V- T$ q; l: q3 u& \- u3 I' o dr["RemainingVisitors"] = "Remaining Visitors: " + ((DataRow)row)["RemainingVisitors"].ToString();/ m/ ^% o: ` D0 Z$ I1 N
dt.Rows.Add(dr);4 w5 C8 v# i7 j- X( M. _3 Z: a
}% _3 ?7 [4 {% n
GridView1.DataSource = dt;! o4 v7 u) |$ L4 x5 O: B* K0 r
GridView1.DataBind();. Z; k- t, X! ]* b& G& D
} 8 {3 H7 ~ h& [" W+ D$ b
}, d; f5 w% j7 D* w0 ?
}
" S6 ~7 t; {0 q public ArrayList ConvertDataSetToArrayList()# w; W4 ] S7 e- D! K z" |, L2 G; _
{
8 z/ t, k- Q* c* j SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString()); {& l" H8 v( M
SqlCommand cmd = new SqlCommand();. x- F; @, _% r0 b l/ d' Z: V6 z
if (Session["UserId"] != null && (Session["UserTypeId"] != null && Convert.ToInt32(Session["UserTypeId"]) != 2))
7 e% n; l) m" `8 a1 r2 @+ s {% z& A, p! Y. `0 h
cmd.CommandText = "Select * from tblApp WHERE AppType = '" + Session["UserOSType"] + "' ORDER BY TotalVisitors";
) w2 u: w% s) R6 h7 g9 w: ] }' Q. [. E# g, b& |* G! h
else
j6 w0 b6 ^6 }, L7 V- D( ~3 a {
) ]- f+ F9 d4 b: R+ S6 ` cmd.CommandText = "Select * from tblApp ORDER BY TotalVisitors"; & `( M0 z i! D1 E
}/ d. W3 M" u& ~6 }7 v' F
cmd.Connection = sqlcon;- u7 l9 E+ ?: q d+ ~
sqlcon.Open();- _/ n. w- a K. ?+ \
cmd.ExecuteNonQuery();
! ~: I1 o! _% \ SqlDataAdapter da = new SqlDataAdapter();
4 l, Y# g& }/ i0 v. G8 @4 D da.SelectCommand = cmd;$ p; a1 L2 R* v9 [" c* S% K
DataSet dsApp = new DataSet();
% x N1 p! d- X; W: v, y5 F da.Fill(dsApp, "tblApp");
) [2 a+ A W6 Z ArrayList myArrayList = new ArrayList();/ t0 B) v, a7 L
foreach (DataRow dtRow in dsApp.Tables[0].Rows)8 n" ]) {+ t" C* I& }5 n" [5 O
{2 R1 S) V# }4 \) H
myArrayList.Add(dtRow);3 d F g& L9 e" c) E0 j2 o
}3 D6 \& R4 }4 X8 I
sqlcon.Close();
5 P) `; f, m& W) e return myArrayList;
. H* o8 R* R. `& y) R }
F2 n1 W e3 U4 I7 r- ? protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
( s3 L! s3 U& z9 k" @4 B$ u { A5 L- B C) [
if (e.CommandName == "download"). C z( ` G9 v( |9 D" _
{
0 Z' d( h$ _3 E/ E Button ib = (Button)e.CommandSource;
, k, S6 w3 [( d+ v int index = Convert.ToInt32(ib.CommandArgument);3 K( {2 [$ L( V
GridViewRow row = GridView1.Rows[index];
0 S( D2 x0 r/ b9 V$ b8 H( T Label l2 = (Label)row.FindControl("Label2");
/ V G0 N# H/ M* m/ E" F; n Label lbTotallVisitors = (Label)row.FindControl("Label4"); J6 q n1 c3 z9 O3 O' u V
Label lblTotalDownloads = (Label)row.FindControl("Label5");6 ?; n- e: v- L, b! f& @* H
Label lblRemainingVisitors = (Label)row.FindControl("Label6");% `* H" F% Z" U2 |- U+ r- `9 ?
- W9 u2 ~* _9 B+ O1 i' s0 Y& \, o string updateSQL = "UPDATE tblUser SET DownloadedApps = '" + lbl + "', Amount = '" + Session["Amount"] + "', TotalAmount='" + Session["TotalAmount"] + "' WHERE Id= '" + Session["UserId"] + "'";
, q: {5 x3 s' I" a$ ~& u using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString()))0 D Q+ S( q, l- h4 r
{7 U; Y- |+ M0 G- p3 T
using (SqlCommand updateCommand = new SqlCommand(updateSQL, sqlConn))
$ \* D7 w6 q/ U. c: r {
% {- f3 C D# Q sqlConn.Open();
) r$ P( O( _; u: E updateCommand.ExecuteNonQuery();
6 L* z& C3 X" {0 }6 |1 C9 ~ updateCommand.Connection.Close();
k7 }/ N" k! [ }! d: Y* V3 O; c
} : ^5 G; { p. w( q
Response.Redirect(l2.Text);
6 J3 u, Q2 I7 ]0 R! T, x7 j }" g* y: S5 z% F2 L; S
}
* Z8 c7 G/ A' _
3 T9 }7 P; D, t- H/ I% b' C解决方案: |
|