很抱歉发布这个问题,因为它可能对所有人都很愚蠢,但是我没有得到确切的解决方案。
问题是:我的项目中有一个日期时间选择器,它在表单中的3个文本框之后,如果在文本框中没有输入任何文本并在提交时输入,它将给出一个消息(验证)该数据为输入。同样,如果未选择日期,则应继续进行。
要执行此操作的代码是什么,适用于其他文本框但不适用于datetimepicker控件的代码是:
if (dateInsert.Value.ToString() = string.Empty)
{
MessageBox.Show("Please select date!");
dateInsert.Focus();
return;
}
Run Code Online (Sandbox Code Playgroud) 如何从数据库中将数据加载到组合框中?我想在表单中的组合框中显示supportID.我正在使用的代码粘贴在这里.我在formload中调用BindData().我得到例外:无法绑定到新的显示成员.参数名称:newDisplayMember.我用的代码是:
public void BindData()
{
SqlConnection con = new SqlConnection(@"server=RSTT2; database = Project ; User Id=sa; Password=PeaTeaCee5#");
con.Open();
string strCmd = "select supportID from Support";
SqlCommand cmd = new SqlCommand(strCmd, con);
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataSet ds = new DataSet();
da.Fill(ds);
cbSupportID.DataSource = ds;
cbSupportID.DisplayMember = "supportID";
cbSupportID.ValueMember = "supportID";
cbSupportID.Enabled = true;
cmd.ExecuteNonQuery();
con.Close();
}
Run Code Online (Sandbox Code Playgroud) 当我使用下面的代码时,如果用户名和密码相同,它工作正常,如果我提供了错误的用户名和密码,它会给我留言或登录:
private void btnSubmit_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = Helper.getconnection();
con.Open();
SqlCommand cmd = new SqlCommand("select SupportName, Password from Logins where SupportName='" + txtSupportName.Text + "' and Password='" + txtPassword.Text + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
string Name = txtSupportName.Text;
string Pwd = txtPassword.Text;
while (dr.Read())
{
if ((dr["SupportName"].ToString() == Name) && (dr["Password"].ToString() == Pwd))
{
// MessageBox.Show("welcome");
Form Support = new Support();
Support.ShowDialog();
}
else
{
MessageBox.Show("SupportName and password are invalid");
}
}
dr.Close(); …Run Code Online (Sandbox Code Playgroud) 当我使用下面的代码时,循环迭代两次,我收到错误消息"变量名'@ projectName1'已经被声明.变量名在查询批处理或存储过程中必须是唯一的." 并重置datagridview和表中的表的所有值.实际上我想通过选择单元格来更新表单中的DataGridView,它也应该在数据库中反映出来.
private void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection con = Helper.getconnection();
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
string myCmd = string.Empty;
foreach (DataGridViewRow myDgrow in dataGridView2.Rows)
{
myCmd = "Update Details set ProjectName='" + myDgrow.Cells["ProjectName"].Value + "', Description = '" + myDgrow.Cells["Description"].Value + "', DateStarted='" + myDgrow.Cells["DateStarted"].Value + "',TeamSize='" + myDgrow.Cells["TeamSize"].Value + "',Manager='" + myDgrow.Cells["Manager"].Value + "'";
cmd.Parameters.AddWithValue("@projectName1", myDgrow.Cells["ProjectName"].Value);
cmd.Parameters.AddWithValue("@Description1", myDgrow.Cells["Description"].Value);
cmd.Parameters.AddWithValue("@DateStarted1", myDgrow.Cells["DateStarted"].Value);
cmd.Parameters.AddWithValue("@TeamSize1", myDgrow.Cells["TeamSize"].Value);
cmd.Parameters.AddWithValue("@Manager1", myDgrow.Cells["Manager"].Value);
cmd.CommandText = myCmd;
cmd.ExecuteNonQuery();
dataGridView2.Update();
myCmd = …Run Code Online (Sandbox Code Playgroud)