Get a data random from mysql using C#

Jan 11 2017 12:51 PM
hello master
 a have a problem to get a data from mysql
i have a database like this
idquestion || question || optiona || optionb || optionc || optiond || answer
 
i try to get a random data from mysql to From in c#
 
this my code to get a data from mysql
 
  1. private DataTable tquestion;      
  2.         public string ide = "";      
  3.         public string soale= "";      
  4.         public string ae="";      
  5.         public string be="";      
  6.         public string ce="";      
  7.         public string de="";      
  8.         public string je="";      
  9.         public string NISe;    
  10.     
  11. public void getdata()    
  12. {      
  13.             ide = tquestion.Rows[0].ItemArray[0].ToString();    
  14.             soale = tquestion.Rows[0].ItemArray[1].ToString();    
  15.             ae = tquestion.Rows[0].ItemArray[1].ToString();    
  16.             be = tquestion.Rows[0].ItemArray[2].ToString();    
  17.             ce = tquestion.Rows[0].ItemArray[3].ToString();    
  18.             de = tquestion.Rows[0].ItemArray[4].ToString();    
  19.             sql = "SELECT * FROM question ORDER BY RAND() LIMIT 1";    
  20.             tquestion = koneksi.tampil_data(sql);    
  21.             textBox1.Text = soale;    
  22.             rbA.Text = ae;    
  23.             rbB.Text = be;    
  24.             rbC.Text = ce;    
  25.             rbD.Text = de;    
  26.     
  27. }   
this the erorr form c#
  1. System.NullReferenceException was unhandled  
  2.   HResult=-2147467261  
  3.   Message=Object reference not set to an instance of an object.  
  4.   Source=sistem_basis_data  
  5.   StackTrace:  
  6.        at sistem_basis_data.home.ambilsoal() in c:\Users\Asus\Documents\client\sistem_basis_data24\sistem_basis_data\sistem_basis_data\home\home.cs:line 83  
  7.        at sistem_basis_data.home.home_Load(Object sender, EventArgs e) in c:\Users\Asus\Documents\client\sistem_basis_data24\sistem_basis_data\sistem_basis_data\home\home.cs:line 70  
  8.        at System.Windows.Forms.Form.OnLoad(EventArgs e)  
  9.        at System.Windows.Forms.Form.OnCreateControl()  
  10.        at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)  
  11.        at System.Windows.Forms.Control.CreateControl()  
  12.        at System.Windows.Forms.Control.WmShowWindow(Message& m)  
  13.        at System.Windows.Forms.Control.WndProc(Message& m)  
  14.        at System.Windows.Forms.ScrollableControl.WndProc(Message& m)  
  15.        at System.Windows.Forms.Form.WmShowWindow(Message& m)  
  16.        at System.Windows.Forms.Form.WndProc(Message& m)  
  17.        at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)  
  18.        at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)  
  19.        at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)  
  20.   InnerException:  
 
 
 

Answers (3)