我在“int rowAffected = cmd.ExecuteNonQuery()”中得到NullReferenceException。有谁可以请解决我的问题 [英] I am getting NullReferenceException at "int rowAffected=cmd.ExecuteNonQuery()" could anyone please solve my problem
问题描述
protected void btnFinish_Click(object sender,EventArgs e)
{
Panel6.Visible = true;
string fileType = filePhoto.PostedFile .ContentType;
//Response.Write(fileType);
if(fileType ==image / jpeg|| fileType ==image / jpeg|| fileType ==image / gif|| fileType ==image / png|| fileType ==image / bmp)
{
string fileName = System .IO.Path.GetFileName(filePhoto.PostedFile.FileName);
string serverFolderPath = Server.MapPath(Photos \\);
if(!Directory.Exists(serverFolderPath))
Directory.CreateDirectory(serverFolderPath);
string serverFileName = serverFolderPath + fileName;
filePhoto.SaveAs(serverFileName);
lblPhoto.Text =文件已成功上传;
}
其他
{
lblPhoto.Text =请选择* .jpeg / * .jpg / * .png / * .gif / * .bmp类型的文件!!! ;
} < br $>
ViewState [Pic] =Photos /+ filePhoto.PostedFile.FileName;
string photoPath = Convert.ToString(ViewState [Pic]);
lblDetails.Text + =
< img width ='150px'height ='100px'src ='+ photoPath +'>; } protected void btnConfirm_Click(object sender,EventArgs e) { string photo = Convert.ToString(ViewState [Pic]); // photo = ViewState [Pic ] .ToString(); Label1.Text = photo; if(cn.State!= ConnectionState.Open) cn.Open (); strSqlCommand =instert到demo(id,img)值(1,+ photo +); int rowAffected = cmd.ExecuteNonQuery(); if(rowAffected> 0) { Label1.Text + =inserted; //Label1.Text + =+ image; } 其他 Label1.Text =未完成; } |
protected void btnFinish_Click(object sender, EventArgs e)
{
Panel6.Visible = true;
string fileType = filePhoto.PostedFile.ContentType;
//Response.Write(fileType);
if (fileType == "image/jpeg " || fileType == "image/jpeg" || fileType == "image/gif" || fileType == "image/png" || fileType == "image/bmp")
{
string fileName = System.IO.Path.GetFileName(filePhoto.PostedFile.FileName);
string serverFolderPath = Server.MapPath("Photos\\");
if (!Directory.Exists(serverFolderPath))
Directory.CreateDirectory(serverFolderPath);
string serverFileName = serverFolderPath + fileName;
filePhoto.SaveAs(serverFileName);
lblPhoto.Text = "File Uploaded successfully ";
}
else
{
lblPhoto.Text = " Please Select file of type *.jpeg/*.jpg/*.png/*.gif/*.bmp only!!!";
}
ViewState["Pic"] = "Photos/" + filePhoto.PostedFile.FileName;
string photoPath =Convert.ToString(ViewState["Pic"]);
lblDetails.Text += "
<img width='150px' height='100px' src='" + photoPath + "'>"; } protected void btnConfirm_Click(object sender, EventArgs e) { string photo = Convert.ToString(ViewState["Pic"]); // photo = ViewState["Pic"].ToString(); Label1.Text = photo; if (cn.State != ConnectionState.Open) cn.Open(); strSqlCommand = "instert into demo(id,img) values(1," + photo + ")"; int rowAffected = cmd.ExecuteNonQuery(); if (rowAffected > 0) { Label1.Text += " inserted "; //Label1.Text += " "+image; } else Label1.Text = "not done "; } |
推荐答案
获取空引用的唯一方法是不向 cmd <分配任何内容/ code> - 而你的代码没有。
你创建一个命令字符串:
The only way you will get a null reference there is to not assign anything to thecmd
- and your code doesn't.
You create a command string:
strSqlCommand = "instert into demo(id,img) values(1," + photo + ")";
但是你没有做任何事情。 (并且你不能拼写插入:笑:)
所以试试:
but you don't do anything with it. (and you can't spell "insert" :laugh: )
So try:
strSqlCommand = "INSERT INTO demo (id,img) values(1," + photo + ")";
cmd = new SqlCommand(strSqlCommand, con);
int rowAffected = cmd.ExecuteNonQuery();
但请不要这样做那样!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询:
But please, don't do it that way! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:
strSqlCommand = "INSERT INTO demo (id,img) values(1,@PH)";
cmd = new SqlCommand(strSqlCommand, con);
cmd.Parameters.AddWithValue("@PH", photo);
int rowAffected = cmd.ExecuteNonQuery();
这篇关于我在“int rowAffected = cmd.ExecuteNonQuery()”中得到NullReferenceException。有谁可以请解决我的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!