How to check image is exist or not in database and show in picturebox using combobox value in c# windows...












-2














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....










share|improve this question


















  • 1




    On which line is the exception thrown?
    – Roger Lipscombe
    Nov 22 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 at 22:17


















-2














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....










share|improve this question


















  • 1




    On which line is the exception thrown?
    – Roger Lipscombe
    Nov 22 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 at 22:17
















-2












-2








-2







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....










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 18:25









John

11




11








  • 1




    On which line is the exception thrown?
    – Roger Lipscombe
    Nov 22 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 at 22:17
















  • 1




    On which line is the exception thrown?
    – Roger Lipscombe
    Nov 22 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 at 22:17










1




1




On which line is the exception thrown?
– Roger Lipscombe
Nov 22 at 18:31




On which line is the exception thrown?
– Roger Lipscombe
Nov 22 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 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 at 22:17














1 Answer
1






active

oldest

votes


















0














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());






share|improve this answer























  • thanks for reply. i tried your code but error are same "Parameter is not valid".
    – John
    Nov 22 at 18:50










  • That is the stacktrace?
    – SouXin
    Nov 22 at 18:55










  • what is stacktrace? and how to solve it.
    – John
    Nov 22 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 at 19:10










  • you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
    – SouXin
    Nov 22 at 19:25











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
});


}
});














draft saved

draft discarded


















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









0














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());






share|improve this answer























  • thanks for reply. i tried your code but error are same "Parameter is not valid".
    – John
    Nov 22 at 18:50










  • That is the stacktrace?
    – SouXin
    Nov 22 at 18:55










  • what is stacktrace? and how to solve it.
    – John
    Nov 22 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 at 19:10










  • you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
    – SouXin
    Nov 22 at 19:25
















0














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());






share|improve this answer























  • thanks for reply. i tried your code but error are same "Parameter is not valid".
    – John
    Nov 22 at 18:50










  • That is the stacktrace?
    – SouXin
    Nov 22 at 18:55










  • what is stacktrace? and how to solve it.
    – John
    Nov 22 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 at 19:10










  • you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
    – SouXin
    Nov 22 at 19:25














0












0








0






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());






share|improve this answer














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());







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 19:00

























answered Nov 22 at 18:38









SouXin

1,193612




1,193612












  • thanks for reply. i tried your code but error are same "Parameter is not valid".
    – John
    Nov 22 at 18:50










  • That is the stacktrace?
    – SouXin
    Nov 22 at 18:55










  • what is stacktrace? and how to solve it.
    – John
    Nov 22 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 at 19:10










  • you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
    – SouXin
    Nov 22 at 19:25


















  • thanks for reply. i tried your code but error are same "Parameter is not valid".
    – John
    Nov 22 at 18:50










  • That is the stacktrace?
    – SouXin
    Nov 22 at 18:55










  • what is stacktrace? and how to solve it.
    – John
    Nov 22 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 at 19:10










  • you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
    – SouXin
    Nov 22 at 19:25
















thanks for reply. i tried your code but error are same "Parameter is not valid".
– John
Nov 22 at 18:50




thanks for reply. i tried your code but error are same "Parameter is not valid".
– John
Nov 22 at 18:50












That is the stacktrace?
– SouXin
Nov 22 at 18:55




That is the stacktrace?
– SouXin
Nov 22 at 18:55












what is stacktrace? and how to solve it.
– John
Nov 22 at 19:05




what is stacktrace? and how to solve it.
– John
Nov 22 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 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 at 19:10












you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
– SouXin
Nov 22 at 19:25




you have to check how you bind combobox see: [blogs.msdn.microsoft.com/jaredpar/2006/11/07/…
– SouXin
Nov 22 at 19:25


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo