C#ASP.NET-使用当前值在页面加载后获取客户端输入的新值

tjvv9vkg  于 2022-09-18  发布在  Java
关注(0)|答案(1)|浏览(164)

我在这里遇到了一点问题,在page_load()上,我正在为多个HTML输入设置值:

session = Request.Cookies["Login"];
userid = session.Values["UserID"].ToString();
email = session.Values["Email"].ToString();
username = session.Values["Username"].ToString();
type = session.Values["Type"].ToString();
branchid = session.Values["BranchID"].ToString();
branchName = session.Values["BranchName"].ToString();
ownerID = session.Values["OwnerID"].ToString();

if (Request.QueryString.Count != 0)
{
    if (!Page.IsPostBack)
    {
        carid = Request.QueryString["carid"];

        cmd.CommandText = "select * from Client where BranchID='" + branchid + "'";

        DataTable allclients = new DataTable();
        sda.Fill(allclients);

        if (allclients.Rows.Count > 0)
        {
            foreach (DataRow dr in allclients.Rows)
            {
                ddl_client.Items.Add(dr["client_name"].ToString());
            }
        }

        cmd.CommandText = "select distinct(CompanyName) from CarModels";

        DataTable carcompanies = new DataTable();
        sda.Fill(carcompanies);

        if (carcompanies.Rows.Count > 0)
        {
            foreach (DataRow dr in carcompanies.Rows)
            {
                ddl_car_company.Items.Add(dr["CompanyName"].ToString());
            }
        }

        cmd.CommandText = "select ModelYear from CarModelYear order by ModelyearID desc";

        DataTable modelyear = new DataTable();
        sda.Fill(modelyear);

        if (modelyear.Rows.Count > 0)
        {
            foreach (DataRow dr in modelyear.Rows)
            {
                txt_car_model.Items.Add(dr["ModelYear"].ToString());
            }
        }

        cmd.CommandText = "select * from Car where car_id='" + carid + "' and userid='" + userid + "'";

        dt.Clear();
        dt.Columns.Clear();

        sda.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            txt_car_plate.Value = dt.Rows[0]["car_plate"].ToString();
            ddl_car_company.SelectedValue = dt.Rows[0]["car_company"].ToString();

            cmd.CommandText = "select CarName from CarModels where CompanyName='" + ddl_car_company.SelectedItem.Value + "'";

            DataTable carnames = new DataTable();
            sda.Fill(carnames);

            if (carnames.Rows.Count > 0)
            {
                ddl_car_type.Items.Clear();

                foreach (DataRow dr in carnames.Rows)
                {
                    ddl_car_type.Items.Add(dr["CarName"].ToString());
                }
            }

            txt_car_model.Items.FindByValue(dt.Rows[0]["car_model"].ToString()).Selected = true;
            ddl_car_type.SelectedValue = dt.Rows[0]["car_type"].ToString();
            txt_body_number.Value = dt.Rows[0]["body_number"].ToString();
            txt_car_color.Value = dt.Rows[0]["car_color"].ToString();
            txt_notes.Value = dt.Rows[0]["notes"].ToString();
            clientid = dt.Rows[0]["client_id"].ToString();

            cmd.CommandText = "select * from Client where client_id='" + clientid + "'";

            DataTable client = new DataTable();
            sda.Fill(client);

            if (client.Rows.Count > 0)
            {
                ddl_client.Items.FindByValue(client.Rows[0]["client_name"].ToString()).Selected = true;
                txt_client_cellphone1.Value = client.Rows[0]["client_cellphone"].ToString();
            }
        }
    }
}

这是一个用新值编辑当前值的页面...

现在,对于编辑代码,它只是一个SQL更新命令。

cmd.Parameters.AddWithValue("@plate", Request["txt_car_plate"]);
cmd.Parameters.AddWithValue("@company", Request["ddl_car_company"]);
cmd.Parameters.AddWithValue("@type", Request["ddl_car_type"]);
cmd.Parameters.AddWithValue("@model", Request["txt_car_model"]);
cmd.Parameters.AddWithValue("@body", Request["txt_body_number"]);
cmd.Parameters.AddWithValue("@color", Request["txt_car_color"]);
cmd.Parameters.AddWithValue("@notes", Request["txt_notes"]);
cmd.Parameters.AddWithValue("@cid", clientid);

cmd.CommandText = "update Car set car_plate=@plate,car_company=@company,car_type=@type,car_model=@model,body_number=@body,car_color=@color,notes=@notes,client_id=@cid where car_id='" + carid + "'";

cmd.ExecuteNonQuery();
Response.Redirect("vehicle.aspx?carid=" + carid);

我错过了什么吗?我尝试卸下(!Page.IsPostBack)部件,但出现以下错误:

参数化查询‘(@PLATE nvarchar(11),@COMPANY nvarchar(17),@type nvarchar(2),@mo’需要参数‘@Cid’,但未提供

尽管我用代码打开这个页面:

if (carid.Length > 0)
{
    Response.Redirect("Edit.aspx?carid=" + carid);
}

所以很明显,carid永远不是空的!

请提供任何帮助,我们将不胜感激

kdfy810k

kdfy810k1#

那么,@CID可能是空的。

您是说情况不应该是这样,因为carid不是空的。但这并不重要。

您正在用这个代码‘填满’@CID

cmd.Parameters.AddWithValue("@cid", clientid);

并将客户端ID分配给

clientid = dt.Rows[0]["client_id"].ToString();

这可能就是你的问题所在。

相关问题