TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Joel Bwana
NA
24
1.9k
Loading Data from database
Sep 22 2020 5:57 PM
I am new to C#.
I have an sql client database. The database has related records(same authcode).This authcode is not my primary key.
What i want to achieve is that when a specific form loads, I want to display the related records , one at atime.
I will click next to move to the next record.
At the moment, it goes to the last record!
Here is my code: NOTE I have omitted unnecessary parts of the code.
public
EditQuestions()
{
InitializeComponent();
}
int
index = 0;
private
void
EditQuestions_Load(
object
sender, EventArgs e)
{
txtauthorizationcode.Text = EditQuizDash.authcode;
//This basically transfer the authcode from another form
try
{
conn.Open();
var query =
"select * FROM Questions WHERE AuthorizationCode='"
+ txtauthorizationcode.Text +
"'"
;
var cmd =
new
SqlCommand(query, conn);
SqlDataReader dr = cmd.ExecuteReader();
var sSingle =
"Single Choice"
;
var sMultiple =
"Multiple Choice"
;
var sTrueFalse =
"TrueFalse"
;
while
(dr.Read())
{
index = 0;
if
(dr.GetString(9) == sSingle)
{
grpsingleanswers.Visible =
true
;
grpmultipleanswers.Visible =
false
;
txtquestion.Text = dr.GetString(2);
cmbqtype.SelectedItem = dr.GetString(9);
cmbcomplexity.SelectedItem = dr.GetString(10);
lblscore.Text = dr.GetInt32(11).ToString();
lblquestionlable.Text =
"Question "
+ dr.GetInt32(0).ToString();
}
showData(index);
if
(dr.GetString(9) == sMultiple)
{
grpmultipleanswers.Visible =
true
;
grpsingleanswers.Visible =
false
;
txtquestion.Text = dr.GetString(2);
cmbqtype.SelectedItem = dr.GetString(9);
cmbcomplexity.SelectedItem = dr.GetString(10);
lblscore.Text = dr.GetInt32(11).ToString();
lblquestionlable.Text =
"Question "
+ dr.GetInt32(0).ToString();
}
showData(index);
if
(dr.GetString(9) == sTrueFalse)
{
grpmultipleanswers.Visible =
false
;
grpsingleanswers.Visible =
false
;
grptruefalse.Visible =
true
;
txtquestion.Text = dr.GetString(2);
cmbqtype.SelectedItem = dr.GetString(9);
cmbcomplexity.SelectedItem = dr.GetString(10);
lblscore.Text = dr.GetInt32(11).ToString();
lblquestionlable.Text =
"Question "
+ dr.GetInt32(0).ToString();
}
showData(index);
}
conn.Close();
}
catch
(Exception ex)
{
MessageBox.Show(
"Error Occured"
+ ex);
}
}
Reply
Answers (
1
)
How to get selected rowindex of datagridview
How to disable datagridview sorting