为什么我这样子存储到数据库中的图片名只有images/ 是哪里错了呢?name是全局变量,但是好像name没有值(也就是87行) 但是在上传的那个函数(也就是button5_click())25行处时 name已经存有图片路径了,大家帮我看看啊!
1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using System.Data.SqlClient;
14 using System.IO;
15 using System.Text;
16
17
18 public partial class register : System.Web.UI.Page
19 {
20 public string name;
21 protected void Page_Load(object sender, EventArgs e)
22 {
23
24 }
25 protected void Button5_Click(object sender, EventArgs e)
26 {
27 if (FileUpload1.PostedFile.FileName != "")
28 {
29 Image1.Visible = false;
30 string fullname = this.FileUpload1.FileName;
31
32 FileInfo f = new FileInfo(fullname);
33 name = f.Name;
34 string type = f.Extension;
35 if (".gif" == type || ".jpg" == type || ".GIF" == type || ".JPG" == type)
36 {
37 string savapath = Server.MapPath("images\\" + name);
38 this.FileUpload1.PostedFile.SaveAs(savapath);
39 this.Image1.Visible = true;
40 this.Image1.ImageUrl = savapath;
41 }
42 else
43 {
44 Response.Write("<script>alert('请选择GIF和JPG格式图片')</script>");
45 }
46
47 }
48 }
49
50 protected void Button1_Click(object sender, EventArgs e)
51 {
52
53 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
54 con.Open();
55 string sql = "select username from register where username=@username";
56 SqlCommand cmd = new SqlCommand(sql,con);
57 cmd.Parameters.Add("@username",SqlDbType.NChar);
58 cmd.Parameters["@username"].Value = this.TextBox1.Text.ToString();
59 SqlDataReader dr = cmd.ExecuteReader();
60 if (dr.Read())
61 {
62 Response.Write("<script>alert('用户名存在!')</script>");
63 return;
64 }
65 dr.Close();
66 string sqltext = "insert into register(username,sex,age,password,repassword,email,question,answer,telephone,photo) values(@username,@sex,@age,@password,@repassword,@email,@question,@answer,@telephone,@photo)";
67 SqlCommand scmd = new SqlCommand(sqltext,con);
68 scmd.Parameters.Add("@username",SqlDbType.NChar,12);
69 scmd.Parameters["@username"].Value = this.TextBox1.Text.ToString();
70 scmd.Parameters.Add("@sex",SqlDbType.NChar,2);
71 scmd.Parameters["@sex"].Value = this.RadioButtonList1.SelectedValue.ToString();
72 scmd.Parameters.Add("@age",SqlDbType.Int);
73 scmd.Parameters["@age"].Value = int.Parse(this.TextBox4.Text);
74 scmd.Parameters.Add("@password",SqlDbType.NChar,12);
75 scmd.Parameters["@password"].Value = this.TextBox2.Text.ToString();
76 scmd.Parameters.Add("@repassword", SqlDbType.NChar, 12);
77 scmd.Parameters["@repassword"].Value = this.TextBox3.Text.ToString();
78 scmd.Parameters.Add("@email",SqlDbType.NChar,20);
79 scmd.Parameters["@email"].Value = this.TextBox5.Text.ToString();
80 scmd.Parameters.Add("@question",SqlDbType.NChar,1000);
81 scmd.Parameters["@question"].Value = this.TextBox7.Text.ToString();
82 scmd.Parameters.Add("@answer",SqlDbType.NChar,1000);
83 scmd.Parameters["@answer"].Value = this.TextBox8.Text.ToString();
84 scmd.Parameters.Add("@telephone",SqlDbType.NChar,11);
85 scmd.Parameters["@telephone"].Value = this.TextBox6.Text.ToString();
86 scmd.Parameters.Add("@photo",SqlDbType.VarChar,10);
87 scmd.Parameters["@photo"].Value ="images/"+name; //也就是这里的name
88 try
89 {
90
91 scmd.ExecuteNonQuery();
92
93 Response.Write("<script>alert('注册成功!')</script>");
94
95 }
96 catch (Exception error)
97 {
98 throw new System.Exception(error.Message);
99 }
100
101
102 }
103
104 protected void Button2_Click(object sender, EventArgs e)
105 {
106 Response.Redirect("Default.aspx");
107 }
108 protected void Button3_Click(object sender, EventArgs e)
109 {
110 Response.Redirect("Default.aspx");
111 }
112 protected void Button4_Click(object sender, EventArgs e)
113 {
114 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
115 string sql = "select count(*) from register where username = '" + TextBox1.Text + "'";
116 con.Open();
117 SqlCommand cmd1=new SqlCommand(sql,con);
118 int count = Convert.ToInt32(cmd1.ExecuteScalar());
119 if (count > 0)
120 {
121 Response.Write("<script>alert('用户名不可用!');</script>");
122 }
123 else
124 {
125
126 Response.Write("<script>alert('用户名可以使用!');</script>");
127 }
128 con.Close();
129 }
130
131
132
133 }
将name设置成static就可以了:
private static string name="";
这样可以name赋值后,刷新页面不会导致name值丢失.
嗯,谢谢你啊!现在可以了啊!
不用客户,相互学习嘛。。。
页面刷新之后,以前的private变量就被重新赋值了。
那该怎么办呢?我是想把注册上传的图片的路径保存到数据库啊!
页面刷新赋值清空了,请问下你Button5_Click 的事件是做什么用的,就为了赋值啊?用Button5_Click 和Button2_Click 两个事件 这两个不可以合并么?如果非取不可不能用方法以参数的形式么?
嗯 可以了!
把Button5跟1合并 name在一个Button里设成private
恩,这样的也可以!