ASP.NET中中怎样根据dropdownlist选中项查询数据库

ASP.NET中中怎样根据dropdownlist选中项查询数据库,下面有我的代码,求大神帮我看看错哪了,总是报错:输入字符串的格式不正确

3个回答

 this.DropDownList1.SelectedValue

private void BindPagedMeetings(int pageIndex)
{
string deviceName = txtDeviceName.Text;
string deviceNum = txtDeviceNum.Text;
string deviceModel = txtDeviceModel.Text;
string deviceManufacturer = txtDeviceManufacturer.Text;
string start1 = txtStartFromDate.Text;
string start2 = txtStartToDate.Text;

        Device device = new Device();
        //Department department = new Department();
        //department.DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue);
        //department.DepartmentName = ddlDepartments.SelectedItem.Text;
        //device.RelatedDepartment = department;
        //device.Status = DeviceStatus.Inactive;

        //State state = new State();
        //state.StateID = Convert.ToInt32(ddlStates.SelectedValue);
        //state.StateName = ddlStates.SelectedItem.Text;
        //device.RelatedState = state;
        //device.Status = DeviceStatus.Inactive;

        //DeviceType deviceType = new DeviceType();
        //deviceType.DeviceTypeID = Convert.ToInt32(ddlDeviceTypes.SelectedValue);
        //deviceType.DeviceTypeName = ddlDeviceTypes.SelectedItem.Text;
        //device.RelatedDeviceType = deviceType;
        //device.Status = DeviceStatus.Inactive;

        //Standard standard = new Standard();
        //standard.StandardID = Convert.ToInt32(ddlStandards.SelectedValue);
        //standard.StandardName = ddlStandards.SelectedItem.Text;
        //device.RelatedStandard = standard;
        //device.Status = DeviceStatus.Inactive;

        string departmentID = this.ddlDepartments.SelectedValue;
        string stateID = this.ddlStates.SelectedValue;
        string devicetypeID = this.ddlDeviceTypes.SelectedValue;
        string standardID = this.ddlStandards.SelectedValue;


        int totalResults;               // 返回的会议总行数
        List<Device> results = BLLDevice.SearchPagedDevices(deviceNum, deviceName, deviceModel,
            deviceManufacturer,start1, start2, departmentID, stateID,
             devicetypeID, standardID, pageSize, pageIndex, out totalResults);

        int totalPages = totalResults / pageSize + (totalResults % pageSize == 0 ? 0 : 1);  // 计算总分页数

        if (totalPages == 0)            // 如果没有返回记录
        {
            divNoResults.Visible = true;
            divResults.Visible = false;
        }
        else
        {
            divNoResults.Visible = false;
            divResults.Visible = true;

            lblTotalResults.Text = totalResults.ToString();     // 显示总行数
            lblTotalPages.Text = totalPages.ToString();         // 显示总分页数
            lblCurrentPage.Text = pageIndex.ToString();         // 显示当前页码数
        }

        repEmployees.DataSource = results;
        repEmployees.DataBind();
    }

public static List SearchDevicePaged(string deviceName, string deviceNum,
string deviceModel, string deviceManufacturer, string start1, string start2, string departmentID, string stateID, string deviceTypeID, string standardID,
int pageSize, int pageIndex, out int totalResults)
{
int department1 = Convert.ToInt32(departmentID);
int state1 = Convert.ToInt32(stateID);
int devicetype1 = Convert.ToInt32(deviceTypeID);
int standard1 = Convert.ToInt32(standardID);
string sql = SEARCH_MEETING_PAGED;
List parameters = new List();
if (deviceName != null)
{
sql += " AND DeviceName LIKE @DeviceName";
parameters.Add(new SqlParameter("@DeviceName", deviceName));
}
if (deviceNum != null)
{
sql += " AND DeviceNum LIKE @DeviceNum";
parameters.Add(new SqlParameter("@DeviceNum", deviceNum));
}
if (deviceModel != null)
{
sql += " AND DeviceModel LIKE @DeviceModel";
parameters.Add(new SqlParameter("@DeviceModel", deviceModel));
}
if (deviceManufacturer != null)
{
sql += " AND DeviceManufacturer LIKE @DeviceManufacturer";
parameters.Add(new SqlParameter("@DeviceManufacturer", deviceManufacturer));
}
if (deviceNum != null)
{
sql += " AND DeviceNum LIKE @DeviceNum";
parameters.Add(new SqlParameter("@DeviceNum", deviceNum));
}

if (start1 != null)
{
sql += " AND StartTime>=@Start1";
parameters.Add(new SqlParameter("@Start1", start1));
}
if (start2 != null)
{
sql += " AND StartTime<=@Start2";
parameters.Add(new SqlParameter("@Start2", start2));
}
if (departmentID != null)
{
sql += " AND DepartmentID LIKE @DepartmentID";
parameters.Add(new SqlParameter("@DepartmentID", department1));
}
if (stateID != null)
{
sql += " AND StateID LIKE @StateID";
parameters.Add(new SqlParameter("@StateID", state1));
}
if (deviceTypeID != null)
{
sql += " AND DeviceTypeID LIKE @DeviceTypeID";
parameters.Add(new SqlParameter("@DeviceTypeID", devicetype1));
}
if (standardID != null)
{
sql += " AND StandardID LIKE @StandardID";
parameters.Add(new SqlParameter("@StandardID", standard1));
}

        sql += " SELECT @TotalResults=@@ROWCOUNT";                  // 获得总行数
        SqlParameter paramTotal = new SqlParameter("@TotalResults", SqlDbType.Int);
        paramTotal.Direction = ParameterDirection.Output;
        parameters.Add(paramTotal);

        // 获取本页内的数据
        sql += " SELECT * FROM @tmp";
        sql += " WHERE RowID>" + pageSize * (pageIndex - 1);
        sql += " AND RowID<=" + pageSize * pageIndex;

        List<Device> list = new List<Device>();
        using (SqlConnection conn = new SqlConnection(DBUtil.ConnectionString))
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddRange(parameters.ToArray());
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Device device = new Device();
                device.DeviceID = Convert.ToInt32(dr["DeviceID"]);
                device.DeviceName = dr["DeviceName"].ToString();
                device.DeviceNum = dr["DeviceNum"].ToString();
                device.DeviceModel = dr["DeviceModel"].ToString();
                device.DeviceManufacturer = dr["DeviceManufacturer"].ToString();
                device.DevicePrice = dr["DevicePrice"].ToString();
                device.StartTime = Convert.ToDateTime(dr["StartTime"]);

                Department department = new Department();
                department.DepartmentID = Convert.ToInt32(dr["DepartmentID"]);
                department.DepartmentName = dr["DepartmentName"].ToString();
                Standard standard = new Standard();
                standard.StandardID = Convert.ToInt32(dr["StandardID"]);
                standard.StandardName = dr["StandardName"].ToString();
                State state = new State();
                state.StateID = Convert.ToInt32(dr["StateID"]);
                state.StateName = dr["StateName"].ToString();
                DeviceType deviceType = new DeviceType();
                deviceType.DeviceTypeID = Convert.ToInt32(dr["DeviceTypeID"]);
                deviceType.DeviceTypeName = dr["DeviceTypeName"].ToString();

                list.Add(device);
            }
            dr.Close();
            totalResults = Convert.ToInt32(paramTotal.Value);
        }
        return list;
    }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐