Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using Windows Form Application VS2012
i Have to combobx
name
cbHadithBook
cbBaab

now i am binding it using 3 tier


C#
private void HadithBook() 
        {
            cbHadithBook.DataSource = objSearch.SelectHadithBookName();
            cbHadithBook.DisplayMember = "Book_English_Name";
            cbHadithBook.ValueMember = "Book_Id";
           //dgvHadith.DataSource = objSearch.SelectHadithText(objSearch.HadithBookNo, 1);
        }
private void HadithBaab()
        {
           
            
                MessageBox.Show("NO value selected");
            
                objSearch.HadithBookNo = Convert.ToInt32(cbHadithBook.SelectedValue.ToString());
                cbBaab.DataSource = objSearch.SelectHadithBaabEnglish();
                if (cbLanguage.SelectedIndex == 0 || cbLanguage.SelectedIndex == 1)
                {
                    cbBaab.DisplayMember = "Baab_Urdu_Name";
                }
                else if (cbLanguage.SelectedIndex == 2)
                {
                    cbBaab.DisplayMember = "Baab_English_Name";

                }
                cbBaab.ValueMember = "Baab_Id";

               //here Error input String was not in a correct format
 objSearch.Baab_Id = Convert.ToInt32(cbBaab.SelectedValue.ToString());

                dgvhadiths.DataSource = objSearch.SelectHadithText(objSearch.HadithBookNo, objSearch.Baab_Id);
            }
            
//calling above function here on form Load event
       private void Form1_Load(object sender, EventArgs e)
        {

            //cbLanguage.SelectedIndex = 2;
            cbHadithBook.SelectedValue = 1;
            cbBaab.SelectedValue = 1;
            HadithBook();
            HadithBaab();
}
Posted
Comments
Sergey Alexandrovich Kryukov 17-Oct-13 0:37am    
1) In what line? 2) what's the use if you don't show the string? If you don't know what it is, find out using the debugger. Before you do it, the question is not very answerable.
Maybe, this is
objSearch.HadithBookNo = Convert.ToInt32(cbHadithBook.SelectedValue.ToString());.
If so, tell us what's in cbHadithBook.SelectedValue.ToString(), or the other string which causes the problem. Then we will gladly help you.
—SA
Muhamad Faizan Khan 17-Oct-13 0:40am    
thanks

//here Error input String was not in a correct format
objSearch.Baab_Id = Convert.ToInt32(cbBaab.SelectedValue.ToString());

interger value in comboxbox's vlaue member
Muhamad Faizan Khan 17-Oct-13 0:43am    
objSearch.HadithBookNo = Convert.ToInt32(cbHadithBook.SelectedValue.ToString());
same error also here

interger value in comboxbox's vlaue member
Sergey Alexandrovich Kryukov 17-Oct-13 0:45am    
Use Solution 2. Avoid using Convert (in most cases; there are cases when it's needed, but they are pretty rare)...
—SA

the line where you are facing error :
C#
objSearch.Baab_Id = Convert.ToInt32(cbBaab.SelectedValue.ToString());


Update above line as mentioned below :
C#
int baabValue;

            if (int.TryParse(cbBaab.SelectedValue, out baabValue))
            {
                objSearch.Baab_Id = Convert.ToInt32(cbBaab.SelectedValue.ToString());
            }
 
Share this answer
 
v2
Comments
Muhamad Faizan Khan 17-Oct-13 0:27am    
error the best overladed method match for int.TryParse(string, out int)
has some invalied argument
arguement 1: cannot covert from object to string
CodeBlack 17-Oct-13 0:31am    
Update if condition as mentioned below :
if (int.TryParse(cbBaab.SelectedValue, out baabValue))
Muhamad Faizan Khan 17-Oct-13 0:35am    
sir error as i told you above
error the best overladed method match for int.TryParse(string, out int)
CodeBlack 17-Oct-13 0:39am    
my bad. sorry.

update if condition as below :
if (int.TryParse(cbBaab.SelectedValue.ToString(), out baabValue))
Muhamad Faizan Khan 17-Oct-13 0:46am    
thank you but another error
on the same line
object reference not set to an instance of an object
Check if your text is actually a number before converting it to int.
Use Int.TryParse.
 
Share this answer
 
Comments
Muhamad Faizan Khan 17-Oct-13 0:28am    
on combox value memeber it is integer value. now what to do use int.tryparse?
Sergey Alexandrovich Kryukov 17-Oct-13 0:43am    
Of course, a 5.
—SA
Muhamad Faizan Khan 17-Oct-13 0:49am    
i used this
if (int.TryParse(cbBaab.SelectedValue.ToString(), out baabValue))
{
objSearch.Baab_Id = Convert.ToInt32(cbBaab.SelectedValue.ToString());
}
and got another error
null refrence exception
Sergey Alexandrovich Kryukov 17-Oct-13 0:56am    
All right, then please see Solution 3.
—SA
Abhinav S 17-Oct-13 1:37am    
Thank you SA.
Muhamad Faizan Khan wrote:
…and got another error null reference exception…
I'm afraid to say you ignored my most essential advice: to use the debugger, which you should have done in first place. You really need to learn how to deal with such simple debugging by yourself, because it's absolutely impossible to look for help in all such cases.

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
Comments
Abhinav S 17-Oct-13 1:37am    
My 5.
Sergey Alexandrovich Kryukov 17-Oct-13 1:53am    
Thank you, Abhinav.
—SA
Try this..hope so it will help you

objSearch.Baab_Id = Convert.ToInt32(cbBaab.SelectedIndex.ToString());
 
Share this answer
 
Comments
Muhamad Faizan Khan 17-Oct-13 1:07am    
i dont want to show gridview result on basis of combobox index number

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900