积极答复者
关于索引和长度必须引用该字符串内的位置。

问题
-
关于索引和长度必须引用该字符串内的位置。参数名: length 问题
string stuStr = "select Course as '课程名',(select CourseMark from ScoreInfo where stuno='{0}' and ";
stuStr += "ScoreInfo.CourseID=CourseInfo.CourseID ) as '成绩' from CourseInfo where ClassID='{1}' and semester='{2}'";
stuno = cbxstudent.Text.Substring(cbxstudent.Text.IndexOf("(", 0) + 1, 8);
string strsql = string.Format(stuStr, stuno, gainclassID(cbxStuCls.Text, txtStuYear.Text), cbxStuSemestre.Text);
DataSet dataset3 = new DataSet();
dataset3 = showStuScore(strsql);
dataGridView1.DataSource = dataset3.Tables["show"];
答案
-
尝试这样的看看:
string s = "asasa(asasasa"; s.Substring(s.IndexOf("(", 0) + 1);
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 Bob ShenModerator 2013年6月20日 10:04
-
可能先檢查 cbxstudent.Text 內容會比較好,
如果內容值是 string.Empty 這段程式就會有問題,
另外需要保證 "(" 是存在的,
不然會從字串開頭開始取值,
除非這是你的本意,否則將會是錯誤的結果,
最後建議 sql 不要這樣串字串,
會有 SQL Injection 問題。
Frank
- 已标记为答案 Bob ShenModerator 2013年6月20日 10:04
全部回复
-
你出现的异常应该是ArgumentOutOfRangeException异常,你指示的SubString的length长度过大了,已经超过了cbxstudent.Text的长度了, 你应该试试改成下面:
stuno = cbxstudent.Text.Substring(cbxstudent.Text.IndexOf("(", 0) + 1, cbxstudent.Text.Length-1);
If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. my sample
-
尝试这样的看看:
string s = "asasa(asasasa"; s.Substring(s.IndexOf("(", 0) + 1);
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 Bob ShenModerator 2013年6月20日 10:04
-
可能先檢查 cbxstudent.Text 內容會比較好,
如果內容值是 string.Empty 這段程式就會有問題,
另外需要保證 "(" 是存在的,
不然會從字串開頭開始取值,
除非這是你的本意,否則將會是錯誤的結果,
最後建議 sql 不要這樣串字串,
會有 SQL Injection 問題。
Frank
- 已标记为答案 Bob ShenModerator 2013年6月20日 10:04