how to put dataset primarykey in a textbox..

son

Well-known member
Joined
Feb 21, 2004
Messages
67
hi

i am using vb.net, asp.net and sqlserver 2000 as my backend.. i have SqlConnection,SqlDataAdapters and Dataset on my webform1 (which is my main form).. webform1 caches SqlDataAdapters and the Dataset to be used on other webforms.
Webform2 receives the cache of the SqlDataAdapters and the Dataset. I have a datagrid which is using the cached SqlDataAdapter and the cached Dataset(which i have on my webform1), the datagrid is binded to the Dataset. I also have two textboxes which are(customerTypeid and customerType, which are basically the two column names of the datagrid)and a command button on my webform2 which allows users to add a new row to the datagrid and dataset.

i would like that when the user clicks the command button, that next primary key of the dataset is added to the textbox CustomerTypeId, then updated to the Table in my database..

at the moment the code i have only updates my Table in the database..

my code is as follows.

Code:
these are the cached SqlDataAdapter and Dataset used on this webform
Public adaptCustomerCustomerDemo As New SqlDataAdapter
Public dsCustomerCustomerDemo As New dsCustomerCustomerDemo

this code binds the datagrid to the dataset. The datasource of the datagrid is in the webform2.aspx file 
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Put user code to initialize the page here     
        If Not IsPostBack Then
            dsCustomerCustomerDemo = Cache("dsCustomerCustomerDemo")
            DataGrid1.DataBind()
            CustomerTypeId.Visible = False
            CustomerType.Visible = False
            txtCustomerTypeId.Visible = False
            txtCustomerType.Visible = False
            ButAdd.Visible = True
            ButAdd1.Visible = False
        End If
    End Sub

this codes hides the datagrid when the user clicks the ButAdd_Click1 and shows the txtCustomerTypeId and the  txtCustomerType textboxes and the  ButAdd1 command button

Private Sub ButAdd_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButAdd.Click

        If IsPostBack = True Then
            CustomerTypeId.Visible = True
            CustomerType.Visible = True
            Panel1.Visible = False
            txtCustomerTypeId.Visible = True
            txtCustomerType.Visible = True
            ButAdd.Visible = False
            ButAdd1.Visible = True
        End If
    End Sub


this codes is the Add button which shows when the user is going to add a new row to the Table in the Database and also updates it.
Private Sub ButAdd1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButAdd1.Click
        adaptCustomerCustomerDemo = Cache("adaptCustomerCustomerDemo")


        Dim dscustomerscustomersdemo As New dsCustomerCustomerDemo
        Dim rownew As dsCustomerCustomerDemo.CustomerCustomerDemoRow
        rownew = dsCustomerCustomerDemo.CustomerCustomerDemo.NewCustomerCustomerDemoRow()


        add the record to the row

        rownew.CustomerTypeID = getid(dsCustomerCustomerDemo.Tables("customercustomerdemo"))
        rownew.CustomerType = txtCustomerType.Text

        dsCustomerCustomerDemo.CustomerCustomerDemo.AddCustomerCustomerDemoRow(rownew)
        dsCustomerCustomerDemo.CustomerCustomerDemo.Rows(0).Table.Columns(0).ToString()

        Try
            adaptCustomerCustomerDemo.Update(dsCustomerCustomerDemo, "CustomerCustomerDemo")
            Litstatus.Text = rownew.CustomerType & "Has been added to the database.<br>"
            cleartextboxess()
        Catch ex As Exception
            Litstatus.Text = "An error occured <br>" & _
            ex.Message
        End Try
    End Sub

this is the function which get the next row of the  dataTable 
 Function getid(ByVal dt As DataTable) As Integer       
        Dim nextrow As Integer = dt.Rows.Count + 1

        If IsNothing(dt.Rows.Find(nextrow)) Then Return nextrow

        do an array to see if there is an id for the row to get
        For nextrow = 0 To dt.Rows.Count
            If IsNothing(dt.Rows.Find(nextrow)) Then Return nextrow
        Next

        Return 0

    End Function
    Function cleartextboxess()
        Dim ctrl As Control
        For Each ctrl In Page.Controls
            If TypeOf ctrl Is HtmlForm Then
                Dim subctrl As Control
                For Each subctrl In ctrl.Controls
                    If TypeOf subctrl Is System.Web.UI.WebControls.TextBox Then
                        CType(subctrl, TextBox).Text = ""
                    End If
                Next
            End If
        Next
    End Function

would really appreciate it if someone could tell me how this should be done.. thanks in advance.. sonia
 
Back
Top