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
Ramu Eppakayala
NA
25
2.3k
datatable retreiving first record of the database
Feb 10 2020 11:08 PM
I am performing search operation based on id which is auto incremented when I enter id as 0 it is retrieving data which is having id 1.
This is the search method
public
DataTable Search(
int
code=0)
{
SqlConnection con = sqlConnection; String Query =
"Select * from person"
+ (code > 0 ?
" where code ="
+ code :
""
);
SqlDataAdapter sqlDataAdapter =
new
SqlDataAdapter(Query, con);
{
DataTable dataTable =
new
DataTable(
"Temp"
);
sqlDataAdapter.Fill(dataTable);
con.Close();
return
dataTable;
}
}
This is search button code
private
void
btnSearch_Click(
object
sender, EventArgs e)
{
if
(txtSearchCode.Text.Length>0)
{
DataTable dataTable =
new
SqlHelper().Search(
int
.Parse(txtSearchCode.Text));
if
(dataTable.Rows.Count>0)
{
try
{
txtCode.Text = dataTable.Rows[0][
"code"
].ToString();
txtFirstname.Text = dataTable.Rows[0][
"firstname"
].ToString();
txtLastname.Text = dataTable.Rows[0][
"lastname"
].ToString(); var a = dataTable.Rows[0][
"gender"
].ToString() ==
"Male"
? rbnMale.Checked =
true
: rbnFemale.Checked =
true
;
txtMobile.Text = dataTable.Rows[0][
"mobile"
].ToString();
dtpDOB.Text = dataTable.Rows[0][
"dob"
].ToString();
txtAge.Text = dataTable.Rows[0][
"age"
].ToString();
chkStatus.Checked = dataTable.Rows[0][
"status"
].ToString() ==
"active"
?
true
:
false
;
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show(
"No Data Found"
);
}
}
else
{
MessageBox.Show(
"Please Enter Code"
);
}
}
Reply
Answers (
2
)
Console.Output to Word file
Running a simple loop for displaying and saving once