ANINDYA
asked on
Object reference not set to an instance of an object. --How to set a default value to the combobox
Experts,
I am facing a problem that is when I am not selecting any value from the combobox then in the following function
private void Search_By_FilmName()
{
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQ LEXPRESS; Initial Catalog=CDAnindya;Integrat ed Security=SSPI ");
//string query = "select Table_FilmName.FilmName from Table_FilmName inner join Table_FilmDetail on Table_FilmName.FilmName_ID =Table_Fil mDetail.Fi lmName_ID inner join Table_CD on Table_CD.CDDVD_ID=Table_Fi lmDetail.C D_DVD_ID where Table_FilmName.FilmName LIKE '" + textBox_NameOfFilm.Text + "%'";
string query = "select Table_FilmName.FilmName from Table_FilmName inner join Table_FilmDetail on Table_FilmName.FilmName_ID =Table_Fil mDetail.Fi lmName_ID inner join Table_CD on Table_CD.CDDVD_ID=Table_Fi lmDetail.C D_DVD_ID where Table_FilmName.FilmName LIKE '%" + textBox_NameOfFilm.Text + "%' and Table_FilmDetail.Language LIKE '%" + textBox_LanguageOfFilm.Tex t + "%' and Table_FilmDetail.Category LIKE '" + comboBox_Category.Selected Item.ToStr ing() + "' and Table_FilmDetail.YearOFRea se LIKE '" + comboBox_YearRelease.Selec tedItem.To String() + "' and Table_FilmDetail.Actors_Na me LIKE '%" + textBox_Actors.Text + "%' ";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
checkedListBox_FilmName.It ems.Add(re ader.GetSt ring(0));
}
reader.Close();
}
That is
string query = "select Table_FilmName.FilmName from Table_FilmName inner join Table_FilmDetail on Table_FilmName.FilmName_ID =Table_Fil mDetail.Fi lmName_ID inner join Table_CD on Table_CD.CDDVD_ID=Table_Fi lmDetail.C D_DVD_ID where Table_FilmName.FilmName LIKE '%" + textBox_NameOfFilm.Text + "%' and Table_FilmDetail.Language LIKE '%" + textBox_LanguageOfFilm.Tex t + "%' and Table_FilmDetail.Category LIKE '" + comboBox_Category.Selected Item.ToStr ing() + "' and Table_FilmDetail.YearOFRea se LIKE '" + comboBox_YearRelease.Selec tedItem.To String() + "' and Table_FilmDetail.Actors_Na me LIKE '%" + textBox_Actors.Text + "%' ";
I am facing an error.
Object reference not set to an instance of an object.
The code and also the image is attached.
I know where I am making the mistake.That is
as I am not selecting any value so
comboBox_YearRelease.Selec tedItem.To String()
is creating problem.
y question is how to solve this. That is when I will not select any value for the combobox still the combobox should have null value and that null value should get saved in the table.
Thanking you
anindya
I am facing a problem that is when I am not selecting any value from the combobox then in the following function
private void Search_By_FilmName()
{
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQ
//string query = "select Table_FilmName.FilmName from Table_FilmName inner join Table_FilmDetail on Table_FilmName.FilmName_ID
string query = "select Table_FilmName.FilmName from Table_FilmName inner join Table_FilmDetail on Table_FilmName.FilmName_ID
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
checkedListBox_FilmName.It
}
reader.Close();
}
That is
string query = "select Table_FilmName.FilmName from Table_FilmName inner join Table_FilmDetail on Table_FilmName.FilmName_ID
I am facing an error.
Object reference not set to an instance of an object.
The code and also the image is attached.
I know where I am making the mistake.That is
as I am not selecting any value so
comboBox_YearRelease.Selec
is creating problem.
y question is how to solve this. That is when I will not select any value for the combobox still the combobox should have null value and that null value should get saved in the table.
Thanking you
anindya
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CDShop
{
public partial class Form_FilmCD_Detail : Form
{
public Form_FilmCD_Detail()
{
InitializeComponent();
}
private void Form_FilmCD_Detail_Load(object sender, EventArgs e)
{
label_todayTime.Text = DateTime.Now.ToLongDateString();
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQLEXPRESS;Initial Catalog=CDAnindya;Integrated Security=SSPI");
string query = "select Table_FilmName.FilmName_ID as [FILM NAME ID],Table_FilmName.FilmName AS [FILM NAME],Table_FilmDetail.Film_ID AS [FILM ID],Table_FilmDetail.Language AS [LANGUAGE],Table_FilmDetail.Category AS [CATEGORY],Table_FilmDetail.YearOFRease AS [YEAR OF RELEASE],Table_FilmDetail.Actors_Name AS [ACTORS],Table_FilmDetail.Actress_Name AS [ACTRESS],Table_FilmDetail.Producer_Name AS [PRODUCER],Table_FilmDetail.Director_Name AS [DIRECTOR],Table_FilmDetail.StoryWriter_Name AS [STORY WRITER],Table_FilmDetail.ScriptWriter_Name AS [SCRIPT WRITER],Table_CD.Dealer_ID AS [DEALER ID],Table_CD.DateOfPurchase AS [DATE OF RELEASE],Table_CD.CDDVD_ID AS [CD ID] from Table_FilmName inner join Table_FilmDetail on Table_FilmName.FilmName_ID=Table_FilmDetail.FilmName_ID inner join Table_CD on Table_CD.CDDVD_ID=Table_FilmDetail.CD_DVD_ID";
SqlDataAdapter ad = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
ad.Fill(ds,"Table_FilmDetail");
dataGridView1.DataSource = ds.Tables[0];
}
private void label_Close_Click(object sender, EventArgs e)
{
this.Close();
}
private void textBox_Language_Enter(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQLEXPRESS;Initial Catalog=CDAnindya;Integrated Security=SSPI");
string query = "insert into Table_CD(Dealer_ID,DateOfPurchase,FilmName_ID) values('" + textBox_DealerID.Text + "','" + Convert.ToString(dateTimePicker_DateOfPurchase.Value) + "','" + textBox_FilmNameID.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
}
private void comboBox_FilmCategory_Enter(object sender, EventArgs e)
{
textBox_FilmCDID.Visible = true;
label_CDDVDID.Visible = true;
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQLEXPRESS;Initial Catalog=CDAnindya;Integrated Security=SSPI");
string query = "select * from Table_CD";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
textBox_FilmCDID.Text = string.Format("{0}", reader[0]);
}
}
private void textBox_FilmNameID_Enter(object sender, EventArgs e)
{
}
private void textBox_ProducerName_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_Enter(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQLEXPRESS;Initial Catalog=CDAnindya;Integrated Security=SSPI");
string query = "insert into Table_FilmName(FilmName)values('" + textBox_FilmName.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
}
private void dateTimePicker_DateOfPurchase_Enter(object sender, EventArgs e)
{
label_FilmNameID.Visible = true;
textBox_FilmNameID.Visible = true;
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQLEXPRESS;Initial Catalog=CDAnindya;Integrated Security=SSPI");
string query = "select * from Table_FilmName";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
textBox_FilmNameID.Text = string.Format("{0}", reader[0]);
}
}
private void comboBox_FilmCategory_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button_Save_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQLEXPRESS;Initial Catalog=CDAnindya;Integrated Security=SSPI");
string query = "insert into Table_FilmDetail(FilmName_ID,CD_DVD_ID,Language,Category,YearOFRease,Actors_Name,Actress_Name,Producer_Name,Director_Name,StoryWriter_Name,ScriptWriter_Name) values('" + textBox_FilmNameID.Text + "','" + textBox_FilmCDID.Text + "','" + textBox_Language.Text + "','" + comboBox_FilmCategory.SelectedItem.ToString() + "','" + comboBox_YearOfRelease.SelectedItem.ToString() + "','" + textBox_ActorNames.Text + "','" + textBox_ActressName.Text + "','" + textBox_ProducerName.Text + "','" + textBox_directorName.Text + "','" + textBox_StoryWriterName.Text + "','" + textBox_ScriptwriterName.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
}
private void groupBox_DetailInfoFilm_Enter(object sender, EventArgs e)
{
}
private void checkBox13_CheckedChanged(object sender, EventArgs e)
{
if (checkBox_DoyouWantUpdate.Checked == true)
{
checkBox_ActorName.Visible = true;
checkBox_DirectorName.Visible = true;
checkBox_ScriptWriterName.Visible = true;
checkBox_UpdateActressName.Visible = true;
checkBox_UpdateDateofPurchase.Visible = true;
checkBox_UpdateDealerID.Visible = true;
checkBox_UpdateFilmCategory.Visible = true;
checkBox_updateFilmNAme.Visible = true;
checkBox_UpdateLanguage.Visible = true;
checkBox_UpdateProducerName.Visible = true;
checkBox_UpdateYearOfRelease.Visible = true;
checkBox1_StoryWriterName.Visible = true;
}
else
{
checkBox_ActorName.Visible = false;
checkBox_DirectorName.Visible = false;
checkBox_ScriptWriterName.Visible = false;
checkBox_UpdateActressName.Visible = false;
checkBox_UpdateDateofPurchase.Visible = false;
checkBox_UpdateDealerID.Visible = false;
checkBox_UpdateFilmCategory.Visible = false;
checkBox_updateFilmNAme.Visible = false;
checkBox_UpdateLanguage.Visible = false;
checkBox_UpdateProducerName.Visible = false;
checkBox_UpdateYearOfRelease.Visible = false;
checkBox1_StoryWriterName.Visible = false;
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox8_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox12_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox_updateFilmNAme_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQLEXPRESS; Initial Catalog=CDAnindya;Integrated Security=SSPI ");
string query = " update Table_FilmName set FilmName='" + textBox_FilmName.Text + "' where FilmName_ID='" + Convert.ToInt32(textBox_FilmNameID.Text) + "' ";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader reader = cmd.ExecuteReader();
MessageBox.Show("Film Name is updated.");
reader.Close();
}
private void checkBox_UpdateDealerID_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQLEXPRESS; Initial Catalog=CDAnindya;Integrated Security=SSPI ");
string query = " update Table_CD set Dealer_ID='" + textBox_DealerID.Text + "' where CDDVD_ID='" + Convert.ToInt32(textBox_FilmCDID.Text) + "' ";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader reader = cmd.ExecuteReader();
MessageBox.Show("Film Name is updated.");
reader.Close();
}
private void button_Reset_Click(object sender, EventArgs e)
{
textBox_ActorNames.ResetText();
textBox_ActressName.ResetText();
textBox_DealerID.ResetText();
textBox_directorName.ResetText();
textBox_FilmCDID.ResetText();
textBox_FilmCDID.Visible = false;
textBox_FilmName.ResetText();
textBox_FilmNameID.ResetText();
textBox_FilmNameID.Visible = false;
textBox_Language.ResetText();
textBox_ProducerName.ResetText();
textBox_ScriptwriterName.ResetText();
textBox_StoryWriterName.ResetText();
//checkBox_ActorName.Checked=unchecked;
checkBox_ActorName.Visible=false;
checkBox_DirectorName.Visible = false;
checkBox_ScriptWriterName.Visible = false;
checkBox_UpdateActressName.Visible = false;
checkBox_UpdateDateofPurchase.Visible = false;
checkBox_UpdateDealerID.Visible = false;
checkBox_UpdateFilmCategory.Visible = false;
checkBox_updateFilmNAme.Visible = false;
checkBox_UpdateLanguage.Visible = false;
checkBox_UpdateProducerName.Visible = false;
checkBox_UpdateYearOfRelease.Visible = false;
checkBox1_StoryWriterName.Visible = false;
label_FilmNameID.ResetText();
label_CDDVDID.ResetText();
Form_FilmDetailInformation ffdi = new Form_FilmDetailInformation();
ffdi.TopLevel = false;
panel_FilmDetail.Controls.Add(ffdi);
ffdi.Show();
}
private void button1_Click(object sender, EventArgs e)
{
//if (comboBox_Category.SelectedItem.ToString()=="select")
//{
// MessageBox.Show("anindya");
//}
Search_By_FilmName();
}
private void Search_By_FilmName()
{
SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQLEXPRESS; Initial Catalog=CDAnindya;Integrated Security=SSPI ");
//string query = "select Table_FilmName.FilmName from Table_FilmName inner join Table_FilmDetail on Table_FilmName.FilmName_ID=Table_FilmDetail.FilmName_ID inner join Table_CD on Table_CD.CDDVD_ID=Table_FilmDetail.CD_DVD_ID where Table_FilmName.FilmName LIKE '" + textBox_NameOfFilm.Text + "%'";
string query = "select Table_FilmName.FilmName from Table_FilmName inner join Table_FilmDetail on Table_FilmName.FilmName_ID=Table_FilmDetail.FilmName_ID inner join Table_CD on Table_CD.CDDVD_ID=Table_FilmDetail.CD_DVD_ID where Table_FilmName.FilmName LIKE '%" + textBox_NameOfFilm.Text + "%' and Table_FilmDetail.Language LIKE '%" + textBox_LanguageOfFilm.Text + "%' and Table_FilmDetail.Category LIKE '" + comboBox_Category.SelectedItem.ToString() + "' and Table_FilmDetail.YearOFRease LIKE '" + comboBox_YearRelease.SelectedItem.ToString() + "' and Table_FilmDetail.Actors_Name LIKE '%" + textBox_Actors.Text + "%' ";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
checkedListBox_FilmName.Items.Add(reader.GetString(0));
}
reader.Close();
}
private void textBox_NameOfFilm_Enter(object sender, EventArgs e)
{
}
private void label_CloseIt_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
error.JPG
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanking you,
Anindya Chatterjee
Bangalore
India