后台代码为:string sqlstr;
sqlstr="select img_pk from image where img_name='"+imgName.ToString()+"'";
SqlCommand cm=new SqlCommand(sqlstr,connection);
SqlDataReader dr=cm.ExecuteReader();
我前台有一个这样的超连接,
<a href='imgview.aspx?id= '>看图</a>,另一个页面用Request.Querrystring["id"]得到id的值,请问前台的id= 后面该怎么写??页面之间的传值能这样实现吗,如果不能,那用什么办法???拜托各位大哥了!
sqlstr="select img_pk from image where img_name='"+imgName.ToString()+"'";
SqlCommand cm=new SqlCommand(sqlstr,connection);
SqlDataReader dr=cm.ExecuteReader();
我前台有一个这样的超连接,
<a href='imgview.aspx?id= '>看图</a>,另一个页面用Request.Querrystring["id"]得到id的值,请问前台的id= 后面该怎么写??页面之间的传值能这样实现吗,如果不能,那用什么办法???拜托各位大哥了!
提示dr在命名空间中不存在,于是我在后台的最前面加上:
protected System.Data.SqlClient.SqlDataReader dr;
结果提示:
System.NullReferenceException: 未将对象引用设置到对象的实例
private void Button1_Click(object sender, System.EventArgs e)
{
Stream imgStream;
int imgLen;
string imgName_value;
string imgContentType;
string imgUploadedName;
imgStream = UploadFile.PostedFile.InputStream;
imgLen = UploadFile.PostedFile.ContentLength;
imgUploadedName = UploadFile.PostedFile.FileName;
byte[] imgBinaryData=new byte[imgLen];
imgContentType = UploadFile.PostedFile.ContentType;
imgName_value = imgName.Value;
int n = imgStream.Read(imgBinaryData, 0, imgLen);
int NumRowsAffected = MyDatabaseMethod(imgName_value, imgBinaryData, imgContentType);
string sqlcon= ConfigurationSettings.AppSettings["strcon"];
SqlConnection con = new SqlConnection(sqlcon);
if(NumRowsAffected > 0)
{Response.Write(" <script language=javascript>alert('上传成功');</script> ");}
else
{Response.Write ( "<BR> an error occurred uploading the image.d " );}
}
public int MyDatabaseMethod(string imgName,byte[] imgbin,string imgcontenttype)
{ string sqlcon= ConfigurationSettings.AppSettings["strcon"];
SqlConnection connection = new SqlConnection(sqlcon);
string SQL="INSERT INTO Image (img_name,img_data,img_contenttype) VALUES ( @img_name, @img_data,@img_contenttype )";
SqlCommand command=new SqlCommand ( SQL,connection );
SqlParameter param0=new SqlParameter ( "@img_name", SqlDbType.VarChar,50 );
param0.Value = imgName;
command.Parameters.Add( param0 ); SqlParameter param1=new SqlParameter ( "@img_data", SqlDbType.Image );
param1.Value = imgbin;
command.Parameters.Add( param1 );
SqlParameter param2 =new SqlParameter ( "@img_contenttype", SqlDbType.VarChar,50 );
param2.Value = imgcontenttype;
command.Parameters.Add( param2 );
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
string sqlstr;
sqlstr="select img_pk from image where img_name='"+imgName.ToString()+"'";
SqlCommand cm=new SqlCommand(sqlstr,connection);
dr=cm.ExecuteReader();
dr.Read();
connection.Close();
return numRowsAffected;
}
前台加一隐藏文本框。
<inuput id="hid" runat="server" type="hidden">
<a href='imgview.aspx?id="+hid.value+"'>
后台Button1_Click事件:
this.hid.value=dr["id"].ToString();
我想你的办法是可以实现的,不过我自己想了个办法解决了
在后台里先定义 public int imageid;
然后将imageid=dr["id"];
前台代码写<a href='imgview.aspx?id=<%=imageid%>'>
这样也可以的,谢谢孟老大的提示,