How to check image is exist or not in database and show in picturebox using combobox value in c# windows...
i am using windows form, i write a code for getting a image from database in picture box when combo box value is selected. my code is working correctly when combo box value is select and show the data (only show data that have a image). BUT i have a data without a image, when i select combo box value to show data that have no image, it show me a "ERROR" - "Parameter is not valid".
i tried if condition on it but code don't work for me.
here is the code...
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
using (SQLiteConnection conn = new SQLiteConnection("Data Source=combolist.db;Version=3;"))
{
string CommandText = "SELECT * FROM combo WHERE [Id]=@id";
using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
{
cmd.Parameters.AddWithValue("@id", comboBox1.SelectedItem.ToString());
conn.Open();
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
textBox1.Text = dr["Id"].ToString();
textBox2.Text = dr["FirstName"].ToString();
textBox3.Text = dr["LastName"].ToString();
textBox4.Text = dr["Age"].ToString();
textBox5.Text = dr["Address"].ToString();
byte img = (byte)(dr["Pic"]);
if (img == null)
{
pictureBox1.Image = null;
}
else
{
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
help please....
c# .net database winforms sqlite
add a comment |
i am using windows form, i write a code for getting a image from database in picture box when combo box value is selected. my code is working correctly when combo box value is select and show the data (only show data that have a image). BUT i have a data without a image, when i select combo box value to show data that have no image, it show me a "ERROR" - "Parameter is not valid".
i tried if condition on it but code don't work for me.
here is the code...
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
using (SQLiteConnection conn = new SQLiteConnection("Data Source=combolist.db;Version=3;"))
{
string CommandText = "SELECT * FROM combo WHERE [Id]=@id";
using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
{
cmd.Parameters.AddWithValue("@id", comboBox1.SelectedItem.ToString());
conn.Open();
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
textBox1.Text = dr["Id"].ToString();
textBox2.Text = dr["FirstName"].ToString();
textBox3.Text = dr["LastName"].ToString();
textBox4.Text = dr["Age"].ToString();
textBox5.Text = dr["Address"].ToString();
byte img = (byte)(dr["Pic"]);
if (img == null)
{
pictureBox1.Image = null;
}
else
{
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
help please....
c# .net database winforms sqlite
1
On which line is the exception thrown?
– Roger Lipscombe
Nov 22 '18 at 18:31
tried if condition on it -- So show the code with the condition. And explain wat "don't work for me" means.
– Gert Arnold
Nov 22 '18 at 22:17
add a comment |
i am using windows form, i write a code for getting a image from database in picture box when combo box value is selected. my code is working correctly when combo box value is select and show the data (only show data that have a image). BUT i have a data without a image, when i select combo box value to show data that have no image, it show me a "ERROR" - "Parameter is not valid".
i tried if condition on it but code don't work for me.
here is the code...
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
using (SQLiteConnection conn = new SQLiteConnection("Data Source=combolist.db;Version=3;"))
{
string CommandText = "SELECT * FROM combo WHERE [Id]=@id";
using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
{
cmd.Parameters.AddWithValue("@id", comboBox1.SelectedItem.ToString());
conn.Open();
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
textBox1.Text = dr["Id"].ToString();
textBox2.Text = dr["FirstName"].ToString();
textBox3.Text = dr["LastName"].ToString();
textBox4.Text = dr["Age"].ToString();
textBox5.Text = dr["Address"].ToString();
byte img = (byte)(dr["Pic"]);
if (img == null)
{
pictureBox1.Image = null;
}
else
{
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
help please....
c# .net database winforms sqlite
i am using windows form, i write a code for getting a image from database in picture box when combo box value is selected. my code is working correctly when combo box value is select and show the data (only show data that have a image). BUT i have a data without a image, when i select combo box value to show data that have no image, it show me a "ERROR" - "Parameter is not valid".
i tried if condition on it but code don't work for me.
here is the code...
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
using (SQLiteConnection conn = new SQLiteConnection("Data Source=combolist.db;Version=3;"))
{
string CommandText = "SELECT * FROM combo WHERE [Id]=@id";
using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
{
cmd.Parameters.AddWithValue("@id", comboBox1.SelectedItem.ToString());
conn.Open();
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
textBox1.Text = dr["Id"].ToString();
textBox2.Text = dr["FirstName"].ToString();
textBox3.Text = dr["LastName"].ToString();
textBox4.Text = dr["Age"].ToString();
textBox5.Text = dr["Address"].ToString();
byte img = (byte)(dr["Pic"]);
if (img == null)
{
pictureBox1.Image = null;
}
else
{
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
help please....
c# .net database winforms sqlite
c# .net database winforms sqlite
asked Nov 22 '18 at 18:25
JohnJohn
11
11
1
On which line is the exception thrown?
– Roger Lipscombe
Nov 22 '18 at 18:31
tried if condition on it -- So show the code with the condition. And explain wat "don't work for me" means.
– Gert Arnold
Nov 22 '18 at 22:17
add a comment |
1
On which line is the exception thrown?
– Roger Lipscombe
Nov 22 '18 at 18:31
tried if condition on it -- So show the code with the condition. And explain wat "don't work for me" means.
– Gert Arnold
Nov 22 '18 at 22:17
1
1
On which line is the exception thrown?
– Roger Lipscombe
Nov 22 '18 at 18:31
On which line is the exception thrown?
– Roger Lipscombe
Nov 22 '18 at 18:31
tried if condition on it -- So show the code with the condition. And explain wat "don't work for me" means.
– Gert Arnold
Nov 22 '18 at 22:17
tried if condition on it -- So show the code with the condition. And explain wat "don't work for me" means.
– Gert Arnold
Nov 22 '18 at 22:17
add a comment |
1 Answer
1
active
oldest
votes
This cast byte img = (byte)(dr["Pic"]);
throw the error if value is null.
I would check if dr["Pic"] != null
and then:
if(dr["Pic"] != null){
MemoryStream ms = new MemoryStream((byte)(dr["Pic"]));
pictureBox1.Image = ms != null ? System.Drawing.Image.FromStream(ms) : null;
} else {
pictureBox1.Visible = false;
//or pictureBox1.Image = null;
}
Update:
This: cmd.Parameters.AddWithValue("@id", comboBox1.SelectedItem.ToString());
should be cmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
thanks for reply. i tried your code but error are same "Parameter is not valid".
– John
Nov 22 '18 at 18:50
That is the stacktrace?
– SouXin
Nov 22 '18 at 18:55
what is stacktrace? and how to solve it.
– John
Nov 22 '18 at 19:05
when i update thiscmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
and run the program again show me a error "Object reference is not set to an instance of an object"
– John
Nov 22 '18 at 19:10
you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
– SouXin
Nov 22 '18 at 19:25
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436448%2fhow-to-check-image-is-exist-or-not-in-database-and-show-in-picturebox-using-comb%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This cast byte img = (byte)(dr["Pic"]);
throw the error if value is null.
I would check if dr["Pic"] != null
and then:
if(dr["Pic"] != null){
MemoryStream ms = new MemoryStream((byte)(dr["Pic"]));
pictureBox1.Image = ms != null ? System.Drawing.Image.FromStream(ms) : null;
} else {
pictureBox1.Visible = false;
//or pictureBox1.Image = null;
}
Update:
This: cmd.Parameters.AddWithValue("@id", comboBox1.SelectedItem.ToString());
should be cmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
thanks for reply. i tried your code but error are same "Parameter is not valid".
– John
Nov 22 '18 at 18:50
That is the stacktrace?
– SouXin
Nov 22 '18 at 18:55
what is stacktrace? and how to solve it.
– John
Nov 22 '18 at 19:05
when i update thiscmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
and run the program again show me a error "Object reference is not set to an instance of an object"
– John
Nov 22 '18 at 19:10
you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
– SouXin
Nov 22 '18 at 19:25
|
show 1 more comment
This cast byte img = (byte)(dr["Pic"]);
throw the error if value is null.
I would check if dr["Pic"] != null
and then:
if(dr["Pic"] != null){
MemoryStream ms = new MemoryStream((byte)(dr["Pic"]));
pictureBox1.Image = ms != null ? System.Drawing.Image.FromStream(ms) : null;
} else {
pictureBox1.Visible = false;
//or pictureBox1.Image = null;
}
Update:
This: cmd.Parameters.AddWithValue("@id", comboBox1.SelectedItem.ToString());
should be cmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
thanks for reply. i tried your code but error are same "Parameter is not valid".
– John
Nov 22 '18 at 18:50
That is the stacktrace?
– SouXin
Nov 22 '18 at 18:55
what is stacktrace? and how to solve it.
– John
Nov 22 '18 at 19:05
when i update thiscmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
and run the program again show me a error "Object reference is not set to an instance of an object"
– John
Nov 22 '18 at 19:10
you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
– SouXin
Nov 22 '18 at 19:25
|
show 1 more comment
This cast byte img = (byte)(dr["Pic"]);
throw the error if value is null.
I would check if dr["Pic"] != null
and then:
if(dr["Pic"] != null){
MemoryStream ms = new MemoryStream((byte)(dr["Pic"]));
pictureBox1.Image = ms != null ? System.Drawing.Image.FromStream(ms) : null;
} else {
pictureBox1.Visible = false;
//or pictureBox1.Image = null;
}
Update:
This: cmd.Parameters.AddWithValue("@id", comboBox1.SelectedItem.ToString());
should be cmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
This cast byte img = (byte)(dr["Pic"]);
throw the error if value is null.
I would check if dr["Pic"] != null
and then:
if(dr["Pic"] != null){
MemoryStream ms = new MemoryStream((byte)(dr["Pic"]));
pictureBox1.Image = ms != null ? System.Drawing.Image.FromStream(ms) : null;
} else {
pictureBox1.Visible = false;
//or pictureBox1.Image = null;
}
Update:
This: cmd.Parameters.AddWithValue("@id", comboBox1.SelectedItem.ToString());
should be cmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
edited Nov 22 '18 at 19:00
answered Nov 22 '18 at 18:38
SouXinSouXin
1,237712
1,237712
thanks for reply. i tried your code but error are same "Parameter is not valid".
– John
Nov 22 '18 at 18:50
That is the stacktrace?
– SouXin
Nov 22 '18 at 18:55
what is stacktrace? and how to solve it.
– John
Nov 22 '18 at 19:05
when i update thiscmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
and run the program again show me a error "Object reference is not set to an instance of an object"
– John
Nov 22 '18 at 19:10
you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
– SouXin
Nov 22 '18 at 19:25
|
show 1 more comment
thanks for reply. i tried your code but error are same "Parameter is not valid".
– John
Nov 22 '18 at 18:50
That is the stacktrace?
– SouXin
Nov 22 '18 at 18:55
what is stacktrace? and how to solve it.
– John
Nov 22 '18 at 19:05
when i update thiscmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
and run the program again show me a error "Object reference is not set to an instance of an object"
– John
Nov 22 '18 at 19:10
you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
– SouXin
Nov 22 '18 at 19:25
thanks for reply. i tried your code but error are same "Parameter is not valid".
– John
Nov 22 '18 at 18:50
thanks for reply. i tried your code but error are same "Parameter is not valid".
– John
Nov 22 '18 at 18:50
That is the stacktrace?
– SouXin
Nov 22 '18 at 18:55
That is the stacktrace?
– SouXin
Nov 22 '18 at 18:55
what is stacktrace? and how to solve it.
– John
Nov 22 '18 at 19:05
what is stacktrace? and how to solve it.
– John
Nov 22 '18 at 19:05
when i update this
cmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
and run the program again show me a error "Object reference is not set to an instance of an object"– John
Nov 22 '18 at 19:10
when i update this
cmd.Parameters.AddWithValue("@id", comboBox1.SelectedValue.ToString());
and run the program again show me a error "Object reference is not set to an instance of an object"– John
Nov 22 '18 at 19:10
you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
– SouXin
Nov 22 '18 at 19:25
you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
– SouXin
Nov 22 '18 at 19:25
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436448%2fhow-to-check-image-is-exist-or-not-in-database-and-show-in-picturebox-using-comb%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
On which line is the exception thrown?
– Roger Lipscombe
Nov 22 '18 at 18:31
tried if condition on it -- So show the code with the condition. And explain wat "don't work for me" means.
– Gert Arnold
Nov 22 '18 at 22:17