0

I'm working in a smal project where I'm using userControl displayed within the main form. I'm using a Model class which allow access to database look like:

    public void InsertRoute(Route route)
    {
        string routecode = route.RouteCode.ToString();
        string vehiclecode = route.VehicleCode.ToString();
        string firsstudentname = route.FirstStudentName.ToString();


        using (SqlCommand cmd = new SqlCommand("InsertRoute2", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@RouteCode", routecode));
            cmd.Parameters.Add(new SqlParameter("@VehicleCode", vehiclecode));
            cmd.Parameters.Add(new SqlParameter("@FirstStudentName", firsstudentname));


            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }

then i have a user control called UCCreateRotue which has several textboxes and Button click Event handler :

   private void btnCreateRoute_Click_1(object sender, EventArgs e)
    {
        UCRouteCreate form = new UCRouteCreate();
        //DialogResult dialog = form.ShowDialog();

        //if (dialog == DialogResult.OK)
        //{
            Route route = controller.CreateMovieFromText(form);
            controller.InsertRoute(route);
            //RefreshTable();
        //}

    }

then i have a Class called route as follow

       class Route
{

    private   GlobalReach.UserControls.UCRouteCreate usrCon;
    public string RouteCode
    {
        get { return usrCon.txtRouteCode.Text ;}
        set { usrCon.txtRouteCode.Text = value ; }

    }

}

then i have a method in controller as follow to perform the insert which is done through sproc

        public Route CreateRouteFromText(UCRouteCreate form)
    {

        string routecode =form.txtRouteCode.Text.ToString();
        string vehiclecode = form.txtVehicleCode.Text.ToString();
        string firststudentname = form.txtFStudent.Text.ToString();


        Route route = new Route();
        return route;
    }

my problem is when i click on the button click the code is working as i do have new rows inserted into my table but with empty value i.e the values are entered as nothing (if that make any sense) and the field that i dd not choose to fill entered as NULL value..? could you please advise on why when i enter values in my usrControl TextBox the value is not captured through my code.. thank you in advance

4

1 回答 1

0

按钮单击事件会创建一个空表单,您已经注释掉了显示对话框行,因此输入仍然是空的(除非您将它们初始化为任何内容,但即便如此)。在 CreateRouteFromText 中,您将表单值分配给局部变量,然后创建一个空的 Route 对象并将其传递回 insert 方法。所以你有空行也就不足为奇了。

至少应该将 Route 对象设置为表单具有的内容:

public Route CreateRouteFromText(UCRouteCreate form)
{
    return new Route() 
    { 
        RouteCode = form.txtRouteCode.Text,
        VehicleCode = form.txtVehicleCode.Text,
        FirstStudentName = form.txtFStudent.Text
    }
}
于 2013-05-26T03:05:13.237 回答