将选定的值添加到用户配置文件 [英] add selected value to users profile

查看:104
本文介绍了将选定的值添加到用户配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定我有一个按钮,onclick它减少选择值的数量1.这是工作,下一步它添加选定的选择到用户配置文件中名为Rented的列。
当我运行代码,它告诉我,我错过了一个;从查询,但代码显示没有错误。
这对我很迷惑。















b $ b

  protected void Button2_Click(object sender,EventArgs e)
{
OleDbConnection conn = new OleDbConnection(Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = | DataDirectory | \\ASPNetDB.mdb; Persist Security Info = True);
{
da.InsertCommand = new OleDbCommand(INSERT INTO UserProfile(Rented)VALUES(@Rented)WHERE([UserName] =?),conn);
string dvdrent = DG_Latest.SelectedRow.Cells [1] .Text;
da.InsertCommand.Parameters.AddWithValue(@ Rented,dvdrent);

var myquery = string.Format(UPDATE DVD SET Stock =库存 - 1 WHERE Title =);
var row = DG_Latest.SelectedRow;
var title = row.Cells [1] .Text;
myquery = string.Format(myquery +'{0}',title);

conn.Open();

using(OleDbCommand cmd = new OleDbCommand(myquery,conn))
cmd.ExecuteNonQuery();

da.InsertCommand.ExecuteNonQuery();

conn.Close();
conn.Dispose();
}
}


解决方案

缺少来自sql查询的; 。所以你需要改变它。



myquery = string.Format(myquery +'{0};',title); p>

注意标题参数附加到字符串 myquery 后的字母分号。



另一方面,如果先设置 title ,然后使用以下命令创建整个查询字符串: / p>

  var row = DG_Latest.SelectedRow; 
var title = row.Cells [1] .Text;
var myquery = String.Format(UPDATE DVD SET Stock = Stock - 1 WHERE Title = {0};,title);

编辑:你也错过了INSERT SQL命令的分号。你需要有:

  da.InsertCommand =新的OleDbCommand(INSERT INTO用户配置(租用)VALUES(@Rented)WHERE( [UserName] =?);,conn); 


Ok I have a button and onclick it reduces the number of select value by 1. That is working, next it adds selected choice to a column in the users profile called Rented. When I run the code it tells me I am missing a ; from a query, but the code shows no errors. This is very puzzling to me. If somebody could point out what im doing wrong it would be great.

Code

protected void Button2_Click(object sender, EventArgs e)
{
    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\ASPNetDB.mdb;Persist Security Info=True");
    {
        da.InsertCommand = new OleDbCommand("INSERT INTO UserProfile (Rented) VALUES (@Rented) WHERE ([UserName] = ?)", conn);
        string dvdrent = DG_Latest.SelectedRow.Cells[1].Text;
        da.InsertCommand.Parameters.AddWithValue("@Rented", dvdrent);

        var myquery = string.Format("UPDATE DVD SET Stock = Stock - 1 WHERE Title =  ");
        var row = DG_Latest.SelectedRow;
        var title = row.Cells[1].Text;
        myquery = string.Format(myquery + "'{0}'", title);

        conn.Open();

        using (OleDbCommand cmd = new OleDbCommand(myquery, conn))
            cmd.ExecuteNonQuery();

        da.InsertCommand.ExecuteNonQuery();

        conn.Close();
        conn.Dispose();
    }
}

解决方案

You are missing the ; from the sql query. So you need to change it to this.

myquery = string.Format(myquery + "'{0};'", title);

Notice the literal semi-colon after the title parameter has been appended to the string myquery.

As a side-note it might be easier if you set the title first and then created the whole query string in one go using:

var row = DG_Latest.SelectedRow;
var title = row.Cells[1].Text;
var myquery = String.Format("UPDATE DVD SET Stock = Stock - 1 WHERE Title = {0};", title);

EDIT: You've missed the semi-colon from the INSERT SQL command as well. You need to have:

da.InsertCommand = new OleDbCommand("INSERT INTO UserProfile (Rented) VALUES (@Rented) WHERE ([UserName] = ?);", conn);

这篇关于将选定的值添加到用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆