Veritabanı etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Veritabanı etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

21 Ocak 2015 Çarşamba

Object reference not set to an instance of an object 5

Object reference not set to an instance of an object 5

Complete the catch function


It is time to supplement the use of the catch function to make the error message more attractive. Here is the code:
''' <summary>
''' crash test and how to improve your coding
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load

    End Sub
    ''' <summary>
    ''' pressing button1 will crash he program
    ''' this crash is the typical crashing type for all beginners
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
        Dim aString() As String 'declare and array only


        Try
            'a crash will occur and the code won't be able to handle it
            MsgBox(aString(0)) ' you do something with the array and and array is nothing

        Catch ex As Exception
            MsgBox(ex.StackTrace, MsgBoxStyle.Exclamation, ex.Message) 'add basic error message here
        End Try

       
    End Sub
End Class



Adding arguments to the function Msgbox, we enrich the MsgBox window. Here's what happens when you run the function and the program crashes:
catch MsgBox

Now all the functions that you create will have the function Try and catch function. Throughout your catch function, I recommend you use the StackTrace property with the small exclamation icon. Yes, a picture is worth a thousand words, do not forget.
This technique protection code is very simple and only take 4 lines! Use it.
If you like this post, leave a comment or share it.


Object reference not set to an instance of an object 4





Object reference not set to an instance of an object (part 4)

Complete the catch function


It is time to supplement the use of the catch function to make the error message more attractive. Here is the code:
''' <summary>
''' crash test and how to improve your coding
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load

    End Sub
    ''' <summary>
    ''' pressing button1 will crash he program
    ''' this crash is the typical crashing type for all beginners
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
        Dim aString() As String 'declare and array only


        Try
            'a crash will occur and the code won't be able to handle it
            MsgBox(aString(0)) ' you do something with the array and and array is nothing

        Catch ex As Exception
            MsgBox(ex.StackTrace, MsgBoxStyle.Exclamation, ex.Message) 'add basic error message here
        End Try

       
    End Sub
End Class



Adding arguments to the function Msgbox, we enrich the MsgBox window. Here's what happens when you run the function and the program crashes:
catch MsgBox

Now all the functions that you create will have the function Try and catch function. Throughout your catch function, I recommend you use the StackTrace property with the small exclamation icon. Yes, a picture is worth a thousand words, do not forget.
This technique protection code is very simple and only take 4 lines! Use it.
If you like this post, leave a comment or share it.


Object reference not set to an instance of an object 3

Object reference not set to an instance of an object 3

StackTrace Property


The use of the function catch in the function try is very important. It is so important because it allows the programmer to build programs faster. From my humble opinion, I've always found that too many programmers neglect error handling in their conceptions of computer code. The result is that development times are longer and of lower quality. Employers who submit projects must impose basic techniques for handling errors. Because time is money.

Today, I took the opportunity to change the function catch easily. This change will allow the programmer to better identify and quickly correct errors or stops programs. We will use the StackTrace property. Watch the following code:

''' <summary>
''' crash test and how to improve your coding
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load

    End Sub
    ''' <summary>
    ''' pressing button1 will crash he program
    ''' this crash is the typical crashing type for all beginners
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
        Dim aString() As String 'declare and array only


        Try
            'a crash will occur and the code won't be able to handle it
            MsgBox(aString(0)) ' you do something with the array and and array is nothing

        Catch ex As Exception
            MsgBox(ex.StackTrace) 'add basic error message here
        End Try

       
    End Sub
End Class


Others will write a custom phrase in the Msgbox. For example: "Error in function displays the table, please contact your Mr. XYZ."
In the two cases cited as an example, you will find cumbersome to identify where the error occurred. This is why the StackTrace property displays the function and the line of code where the error occurred.

When generating the error, the error message becomes much more explicit. (See image)
We immediately see the file, the function and the line of code where the error occurred.
So, you could line the line and directly infer the causes of the error message. Ends managements error complicated and customized for each of your many functions.
Eventually the error messages that mean nothing:




Object reference not set to an instance of an object 1





Object reference not set to an instance of an object


One of the biggest mistakes in programming is not allocate space for an object. We often think of declaring variables, but should always ensure that these variables are created. Or, declare a variable does not create the object. She does an object is of type String, Integer, Double, etc.
The following code illustrates the problem well:

''' <summary>
''' crash test and how to improve your coding
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load

    End Sub
    ''' <summary>
    ''' pressing button1 will crash he program
    ''' this crash is the typical crashing type for all beginners
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
        Dim aString() As String 'declare and array only

        'a crash will occur and the code won't be able to handle it
        MsgBox(aString(0)) ' you do something with the array and and array is nothing
       
    End Sub
End Class



When the code reaches the Msgbox, obviously Msgbox function tries to read the first box of the table, however, is zero. I would add that this table cell is not only zero, it does not exist and does not refer to anything.

Obviously, the example is an array, but the problem would be put on any object. Several solutions exist to control our computer codes. Some work better than others depending on the situation.
But here is a programming method that everyone should do at the beginning, use the try.


        Try
            'a crash will occur and the code won't be able to handle it
            MsgBox(aString(0)) ' you do something with the array and and array is nothing
        Catch ex As Exception

        End Try


Try the function says it very well in English, is to try. Try a code because n, is never sure of the result. Early use functions will try to save you time and money. If you run the code to the try function as written, you would notice that the program does not crash like magic. There is no doubt that this is an example that will lead in to another. You can download the sample source code to try and see my future articles. 




The program I love to use:

Sample Code :





101 Adet Visual Basic ve C# Kod Örnekleri İndir

Developer Tools

101 Visual Basic and C# Code Samples

Language:  English            






C# Oledb(Access) Veritabanı İşlemleri Nasıl Yapılır...




C# Oledb(Access) Veritabanı İşlemleri

Arama , Kaydetme , Güncelleme ,Silme işlemleri için bir nevi kaynak niteliğinde bir uygulama hazırladım dileyen en alttaki linkten uygulamayı indirebilir

Not : Kodların işlevleri açıklama satırlarında yazıyor.

Kodlar : 



using System.Data.OleDb;//Veritabanı işlemlerini yaptığımız kütüphane

staticOleDbConnection cn = newOleDbConnection("Provider=Microsoft.Ace.Oledb.12.0;Data Source=|DataDirectory|\\vt.accdb;Persist Security Info=False;");// veritabanı bağlantı kodu ve provider(kaynak dosya yolu)privatevoid button5_Click(object sender, EventArgs e)
{Application.Exit();//çıkış :D}privatevoid button6_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();//temizlemeler}privatevoid button1_Click(object sender, EventArgs e)
{string sql="Insert into Tablo1 (tc_no,adi,soyadi) values ('" + textBox1.Text + "','"+textBox2.Text + "','"+ textBox3.Text + "')";//kaydet sql kodu insert into tabloadi (1.kolon,2.kolon,...) values(değerler)('"+textbox1+"','"+textbox2+"','"textbox...+")'"OleDbCommand cmd = newOleDbCommand(sql,cn);//sql kodunu işliyoruz.(sql kodu,bağlantı)cmd.ExecuteNonQuery();//veritabanına yazdırdık..MessageBox.Show("Kaydedildi..");//mesaj}privatevoid Form1_Load(object sender, EventArgs e)
{
cn.Open();//bağlantı aktif}privatevoid Form1_FormClosing(object sender, FormClosingEventArgs e)
{
cn.Close();// bağlantı pasif}privatevoid button4_Click(object sender, EventArgs e)
{string sql = "Select * from Tablo1 where tc_no='" + textBox1.Text + "'";// sql arama sorgu kodu Select *(tüm değerler exceldeki * ile aynı işlevde :) ) tabloismi where sorgulanan kolon='"+textbox1.text+"'"OleDbCommand cmd = newOleDbCommand(sql, cn);//aynı şekilde sorguyu işleyecek komut (sql,bağlantı)OleDbDataReader reader = cmd.ExecuteReader();//veri okuycusu nu komutumuza eşitledik..reader.Read();//okuyucu aktifif ( reader.HasRows==true) // veri varsa...{
textBox1.Text = reader.GetValue(1).ToString();//textbox1.text= veritabanındaki 1.kolonun stringitextBox2.Text = reader.GetValue(2).ToString();// 2.kolon stringitextBox3.Text = reader.GetValue(3).ToString();// 3.kolon stringi }
}privatevoid button3_Click(object sender, EventArgs e)
{string sql = "Update Tablo1 set adi='" + textBox2.Text + "',soyadi='" + textBox3.Text + "'where tc_no='" + textBox1.Text + "'";// sql güncelleme sorgu kodu update tablo adı set değişecek sütun ='"+textbox1+"',değişecek diğer sütun='"+textbox2+...+ where kontrol edilecek sütun='" + textbox...+"'"OleDbCommand cmd = newOleDbCommand(sql, cn);// sql kodunu işleyecek komutcmd.ExecuteNonQuery();//güncelleniyor (veritabanına yazdık)MessageBox.Show("Güncellendi...");//mesaj}privatevoid button2_Click(object sender, EventArgs e)
{string sql = "Delete from Tablo1 where tc_no='" + textBox1.Text + "'";// silme işlemi sorgu kodu delete (tüm tabloyu silmemek için * koymuyoruz :):) ) from tablo adı where kontrol edilecek sütun='"+ textbox1 + "'"OleDbCommand cmd = newOleDbCommand(sql, cn);//sql kodunu işleyecek komutcmd.ExecuteNonQuery();// komutu yazdırıyoruz (silmek de bi nevi yazmaktır null değer atar :) ) MessageBox.Show("Silindi");//mesaj}

Günün Fırsatı
isim isim isim isim isim isim