C#:帮助返回数据集并为控件赋值
我不太习惯C#sharp,但以前使用过VB.NET 我需要设置查询数据中文本字段、下拉列表等的值。为了输入数据,我使用了一个类Computer,其方法是saveComputer(),该方法从用户控件获取值。现在我想要一个编辑页面,它使用url中的id&使用Computer类中的getComputer(id),并返回要设置给用户控件的值。我不确定是否使用此方法设置控制值 Edit.aspx.csC#:帮助返回数据集并为控件赋值,c#,sql,user-controls,C#,Sql,User Controls,我不太习惯C#sharp,但以前使用过VB.NET 我需要设置查询数据中文本字段、下拉列表等的值。为了输入数据,我使用了一个类Computer,其方法是saveComputer(),该方法从用户控件获取值。现在我想要一个编辑页面,它使用url中的id&使用Computer类中的getComputer(id),并返回要设置给用户控件的值。我不确定是否使用此方法设置控制值 Edit.aspx.cs protected void btnSave_Click(object sender, EventA
protected void btnSave_Click(object sender, EventArgs e)
{
int id = 3; //will be replaced to GET value
Computer comp = new Computer();
//comp.updateComputer(ref id);
}
public getComputer(ref int id)
{
DataSet data = new DataSet();
using (SqlConnection conn = new SqlConnection(
"Server=JURA;Database=ReadyForSeven;User id=;Password="))
{
String sql = "SELECT * FROM computers WHERE id=@id";
//replace contatenation of variable with parameter name
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql.ToString();
cmd.CommandType = CommandType.Text;
//Define SqlParameter object and assign value to the parameter
cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters["@id"].Value = id;
try
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(data);
// return data here
}
}
catch (SqlException ex)
{
//send user to error page and log message
}
}
}
我的电脑课
protected void btnSave_Click(object sender, EventArgs e)
{
int id = 3; //will be replaced to GET value
Computer comp = new Computer();
//comp.updateComputer(ref id);
}
public getComputer(ref int id)
{
DataSet data = new DataSet();
using (SqlConnection conn = new SqlConnection(
"Server=JURA;Database=ReadyForSeven;User id=;Password="))
{
String sql = "SELECT * FROM computers WHERE id=@id";
//replace contatenation of variable with parameter name
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql.ToString();
cmd.CommandType = CommandType.Text;
//Define SqlParameter object and assign value to the parameter
cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters["@id"].Value = id;
try
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(data);
// return data here
}
}
catch (SqlException ex)
{
//send user to error page and log message
}
}
}
所以我想要实现的是使用计算机的getcomputer方法来设置Edit.aspx上控件的值
有人能帮我吗?您需要修改
getComputer
方法以返回数据集,如:
public DataSet getComputer(int id) {
完成后,我们可以调用它,并使用如下内容填充页面加载上的表单控件:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
int id = 3; // get from querystring
DataSet ds = getComputer(id);
DataRow dr = ds.Tables[0].Rows[0]; // get the first row returned
// populate form controls
txtFirstName.Text = dr["FirstName"].ToString();
ddlState.SelectedValue = dr["State"].ToString();
}
}
下面是更新版本的getComputer
,它将始终返回一个值,并且有点紧:
public DataSet getComputer(int id) // you don't need to pass int by ref unless you're planning on changing it inside this method
{
DataSet data = new DataSet();
using (SqlConnection conn = new SqlConnection("Server=JURA;Database=ReadyForSeven;User id=;Password=")) {
using (SqlCommand cmd = new SqlCommand("SELECT * FROM computers WHERE id = @id", conn)) {
cmd.Parameters.AddWithValue("id", id);
using (SqlDataAdapter da = new SqlDataAdapter(cmd)) {
da.Fill(data);
return data;
}
}
}
}
我必须删除try/catch博客,以确保该方法始终返回值。如果您确实需要try/catch块,则需要在方法末尾返回一个空的数据集
,以便正确编译。如果我们不了解您的数据库结构,不了解它的哪些部分是有趣的,也不了解您要为其赋值的控件,则很难提供帮助。谢谢!但我现在从getComputer methodDo'h得到一个并非所有代码路径都返回值的错误,我添加了一个始终返回值的更新版本的getComputer。