这段代码工作得非常好,但是它突然给了我这个错误信息:“Line:940 错误:Sys.WebForms.PageRequestManagerServerErrorException:必须声明标量变量“@UID”。“ 请允许任何人帮我解决这个案子。
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="space">
</div>
<div class="space">
</div>
<div class="regepic">
Registre New User</div>
<br />
<br />
<br />
<br />
<table style="width: 69%; height: 338px;" align="center">
<tr>
<td class="style5"> Användarnamn</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="UsrNme" runat="server" Width="258px" Height="20px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="Userreqlbl" runat="server"
ControlToValidate="UsrNme" ErrorMessage="This field is required" ForeColor="Red"></asp:RequiredFieldValidator><br />
<br />
</td>
</tr>
<tr>
<td class="style5">
Förnamn
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="fnbox" runat="server" Height="20px" Width="258px"
></asp:TextBox>
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td class="style5">
Efternamn
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="lnamebox" runat="server" Height="20px" Width="258px"></asp:TextBox>
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td class="style5">
Lösenord
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="passtxtbx1" runat="server" TextMode="Password" Width="258px" Height="20px"></asp:TextBox>
</td>
<td style="text-align: left">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="passtxtbx1"
ErrorMessage="This part is required" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style5">
Återskrivnings Lösenord
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="passtxtbx2" runat="server" TextMode="Password" Width="258px" Height="20px"></asp:TextBox>
</td>
<td style="text-align: left">
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="passtxtbx1"
ControlToValidate="passtxtbx2" ErrorMessage="The password is not match" ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="style5">
E-post
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="emailbox" runat="server" Height="20px" Width="258px"></asp:TextBox>
</td>
<td style="text-align: left">
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="emailbox"
ErrorMessage="Invalid Format" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style5">
Land
</td>
<td style="text-align: left" class="style8">
<asp:DropDownList ID="DDLCOUNTRY" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSourcecountry" DataTextField="CountID"
DataValueField="CountID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourcecountry" runat="server"
ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"
SelectCommand="SELECT [CountID] FROM [Countries]"></asp:SqlDataSource>
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td class="style5">
Lan
</td>
<td style="text-align: left" class="style8">
<asp:DropDownList ID="DDLSTATE" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSourcestate" DataTextField="Swelan"
DataValueField="Swelan">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourcestate" runat="server"
ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"
SelectCommand="SELECT [Swelan] FROM [Countries]"></asp:SqlDataSource>
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td class="style5">
Stad
</td>
<td style="text-align: left" class="style11">
<asp:TextBox ID="citytxtbox" runat="server" Height="20px" Width="258px"></asp:TextBox>
</td>
<td style="text-align: left" class="style12">
</td>
</tr>
<tr>
<td class="style5">
Postnummer
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="postbox" runat="server" Width="258px" CausesValidation="True"
Height="20px"></asp:TextBox>
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td class="style5">
Personlig bild
</td>
<td style="text-align: left" class="style8">
<asp:FileUpload ID="FileUpload1" runat="server" Width="230px" />
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td>
</tr>
<tr>
<td class="style5">
</td>
<td class="style8">
<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click"
Text="Enter" style="height: 26px" />
</td>
<td>
<asp:Label ID="lblmsg" runat="server"></asp:Label>
</td>
</tr>
</table>
<br />
<br />
<br />
</ContentTemplate>
</asp:UpdatePanel>
string sc = ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString.ToString();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(sc);
cn.Open();
SqlCommand cmd = new SqlCommand();
string sqlstatment= "INSERT INTO UserInfo (UID, FN, LN, Password, RePass, Email, CountID, State, Post, Img) VALUES (@UID,@FN,@LN,@Password,@RePass,@Email,@CountID,@State,@Post,@Img)";
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstatment;
cmd.Connection = cn;
cmd.ExecuteNonQuery();
cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
cmd.Parameters.AddWithValue("@FN", fnbox.Text);
cmd.Parameters.AddWithValue("@LN", lnamebox.Text);
cmd.Parameters.AddWithValue("@Password", passtxtbx1.Text);
cmd.Parameters.AddWithValue("@RePass", passtxtbx2.Text);
cmd.Parameters.AddWithValue("@Email", emailbox.Text);
cmd.Parameters.AddWithValue("@CountID", DDLCOUNTRY.Text);
cmd.Parameters.AddWithValue("@State", DDLSTATE.Text);
cmd.Parameters.AddWithValue("@City", citytxtbox.Text);
cmd.Parameters.AddWithValue("@Post",postbox.Text);
if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
{
FileUpload1.SaveAs(Server.MapPath("~/images/users/" + FileUpload1.FileName));
cmd.Parameters.AddWithValue("@Img", FileUpload1.FileName);
}
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.SelectCommand = cmd;
ad.Fill(ds);
Response.Redirect("User panel.aspx");
}
}
答案 0 :(得分:0)
发生错误是因为您在添加参数值之前执行命令,如果FileUpload控件中没有值,也不会声明@Img参数。
要解决此问题,请使用以下内容替换btnSave_Click方法:
SqlConnection cn = new SqlConnection(sc);
SqlCommand cmd = new SqlCommand();
string sqlstatment = "INSERT INTO TEST1 (UID, FN, LN, Password, RePass, Email, CountID, State,City, Post, Img) VALUES (@UID,@FN,@LN,@Password,@RePass,@Email,@CountID,@State,@City,@Post,@Img)";
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstatment;
//Insert the parameters first
cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
cmd.Parameters.AddWithValue("@FN", fnbox.Text);
cmd.Parameters.AddWithValue("@LN", lnamebox.Text);
cmd.Parameters.AddWithValue("@Password", passtxtbx1.Text);
cmd.Parameters.AddWithValue("@RePass", passtxtbx2.Text);
cmd.Parameters.AddWithValue("@Email", emailbox.Text);
cmd.Parameters.AddWithValue("@CountID", DDLCOUNTRY.Text);
cmd.Parameters.AddWithValue("@State", DDLSTATE.Text);
cmd.Parameters.AddWithValue("@City", citytxtbox.Text);
cmd.Parameters.AddWithValue("@Post", postbox.Text);
//Check if there's a file or not
if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
{
FileUpload1.SaveAs(Server.MapPath("~/images/users/" + FileUpload1.FileName));
cmd.Parameters.AddWithValue("@Img", FileUpload1.FileName);
}
else
{
//Must add the paramater value to any value or DBNull.Value
cmd.Parameters.AddWithValue("@Img", "" );
}
//Open the connection and execute the command
cn.Open();
cmd.ExecuteNonQuery();
//Close the connection
cn.Close();
希望这有帮助。