【发布时间】:2020-12-07 07:30:28
【问题描述】:
我有一个名称为placeofstudent1 的富文本框。我想将富文本框中写入的任何内容存储在一个名为 Place 的变量中,以便我可以将该信息传递到数据库中。
try
{
string name = nameofstudent.Text;
string father = fatherofstudent.Text;
string mother = motherofstudent.Text;
string id = stud_id.Text;
string gender;
if (male.IsChecked == true)
{
gender = "M";
}
else
gender = "F";
string studentAge = ageofstudent.Text;
DateTime dt = Convert.ToDateTime(dob.Text);
int Age = Convert.ToInt16(ageofstudent.Text);
string Place = placeofstudent1.Text;
connect();
con.Open();
string saved = "insert into student_details (student_id,student_name,father_name,mother_name,gender,age,dob,place,class)values('" + id + "', '" + name + "','" + father + "','" + mother + "','" + gender + "','" + Age + "','" + dt + "','" + Place + "','" + x + "')";
SqlCommand cmd = new SqlCommand(saved, con);
cmd.ExecuteReader();
con.Close();
如果我尝试使用.Text 填充变量Place,它不起作用。我可以使用什么方法来实现这一点?感谢您的帮助。
【问题讨论】:
-
您使用了错误的控件。目前您使用的是 WinForms 控件而不是 WPF 控件。您期望富文本内容吗?我对此表示怀疑。对于姓名和其他人员相关数据,您不使用格式化文本。请改用普通的 TextBox,它更轻量级且更易于处理。
-
我被告知要使用富文本框,这就是我使用它的原因。另外,我想学习如何使用它。
-
这是正确的
System.Windows.Controls.RichTextBox(注意不同的命名空间)。您会注意到它没有Text属性。您必须手动将内容(RichTextBox.Document,即FlowDocument)转换为字符串。
标签: c# sql-server wpf richtextbox