Click here to Skip to main content
14,984,271 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi,
i am creating website for online donation. I want donor id should auto generate when user will donate first time and only
registered member can donate.i have use this code to insert data in donate table but i want to generate id automatically.
the database has tables login, member,donate and feedback.
please help

VB
Imports System.Data.SqlClient
Imports System.Data.DataSet
 
Partial Class Donate
    Inherits System.Web.UI.Page
 
    '---------------------------------------------------------------------
    Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
    End Sub
 
    '---------------------------------------------------------------------
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
 
    '---------------------------------------------------------------------
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        con.ConnectionString = "Data Source=COMP-4\SQLEXPRESS;Initial Catalog=mesco;Integrated Security=True"
        cmd.Connection = con
        con.Open()
        cmd.CommandText = "insert into <pre lang="vb">member(name,age,password,confirmpassword,email,secquestion,secanswer)values('" & txtUserName.Text() & "','" & txtAge.Text() & "','" & txtPassword.Text() & "','" & txtConfirmPassword.Text() & "','" & txtEmail.Text() & "','" & txtSecurityQuestion.Text() & "','" & txtSecurityAnswer.Text() & "')"
        cmd.ExecuteNonQuery()
        MsgBox("You have been successfully Register", MsgBoxStyle.Information)
        If txtUserName.Text <> "" Then
            Session("name") = txtUserName.Text
        End If
        If txtPassword.Text <> "" Then
            Session("pass") = txtPassword.Text
        End If
        con.Close()
        txtUserName.Text = " "
        txtAge.Text = ""
        txtPassword.Text = ""
        txtConfirmPassword.Text = ""
        txtEmail.Text = ""
        txtSecurityQuestion.Text = ""
        txtSecurityAnswer.Text = "

"
End Sub
End Class
Posted
Updated 19-Feb-11 23:23pm
v3

Use IDENTITY column as a primary key to get a generated value. Also in your code, use bind variables instead of literal concatenations to be safe from sql injections, datatype errors etc.
   
Make the ID column in the table the primary key, and auto-increment. At that point, the database will automatically generate and increment the value in the column when you add a record.
   
Comments
Momin Neha Ibrahim 20-Feb-11 5:21am
   
Hey John i have did the same thing but it is not showing me id number and when i check in to my database it is giving me Null value i have use bigint
[no name] 21-Feb-11 3:07am
   
check out your increase property of id column.

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




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