how to get the answers sequentially

how to get the answers sequentially with one text box seven or lesser

Page 1 of 1

2 Replies - 916 Views - Last Post: 28 October 2008 - 08:24 AM Rate Topic: -----

#1 nirmala82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-September 08

how to get the answers sequentially

Post icon  Posted 27 October 2008 - 08:27 PM

hi frends actually my task is to create the question and answer form,in which question shld be retrived from database for each time the user enters new questions,then type the question,then if the anstype is image or text ,if the user chooses image means the picturebox with browse option and save option should come,and if it is text add ans with textbox should come,then one more important thing is the user has to choose the number of answers,if the answers for the quesiton is 2,then with only one text box answers should be entered in sequence, ie.,first answer then enter key the answer shld be listed on the listbox,then shld come to the textbox box againt o get the sec ans,after geting the sec ans,the ans should get inserted in the list box,suppose if the user want to delete the answer means click the particular ans in the listbox and delete that, my database design is question with qid,numberofans,anstype,questionname, and answe with ansid,quesitonid,numberofanswers,anstype,answer1,answe2,answer3,answer4,answer7,answer6,answer7,and the same for picture with picid,quesitonname,numberofanswer,anstype,

The conditions is:

If the user chooses anstype as text means,the answers shld get into the answer table with the number of answers,suppose if the user chooses two answers means,after giving two answers,the user should be prompted with u have choosen only two answers,ur limit finished cannot enter third answer,if the user chooses anstype as picture means,the answers should be included in picutere table,like the same as of answer,depeds on the number of pictures choosing the user shld enter the pictures,can anyone help me to handle this,i m totaly new to vb.net,pls if anyone helping,kindly appreciatable...thanks....but anyhow i have did someofthe coding but not working good....
Public Class frmSetQuestions
	Inherits System.Windows.Forms.Form
	Dim dt As DataTable
	Dim Questionid As Integer = 0
	Public cbxansid As Integer = 0
	Dim Sqlcon As New SqlConnection(connString)

Public selAns As Integer
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		lbquestions.Visible = False
		cbxQtype.SelectedItem = "Text"
		pbimage.Visible = False
		lblbrowspic.Visible = False
		btnimage.Visible = False
		btnAddQuestion.Visible = True
		lblansenter.Visible = True
		txtAnsplain.Visible = True
		txtans2.Visible = True
		txtans3.Visible = True
		txtans4.Visible = True
		txtans5.Visible = True
		txtans6.Visible = True
		txtans7.Visible = True
		btnAddAns.Visible = True
		btnimage.Visible = False
		pbimage.Visible = False
		lblbrowspic.Visible = False
		btnimage.Visible = False
		btnimgsave.Visible = False
		txtQid.Text = Questionid
		
		PopulateQuestiontList()
		PopulateForm()
		initCombox()
		'Shell("C:\WINDOWS\system32\calc.exe", AppWinStyle.MaximizedFocus)
	End Sub
	Private Sub PopulateForm()
		Dim Sqlcon As New SqlConnection(connString)
		Dim cmSQL As SqlCommand
		Dim drSQL As SqlDataReader
		Dim strSQL As String
		Dim objListItem As ListItem
		Dim strID As String
		If Sqlcon.State = ConnectionState.Open Then
			Sqlcon.Close()
		End If
		Sqlcon.Open()
		Try

			' Get Primary Key from Listbox
			objListItem = CType(lbquestions.SelectedItem, ListItem)
			' Build Select statement to query product information from the products
			' table 
			strSQL = "SELECT Qid, " & _
					 "	   QuestionName, " & _
					 "	   Anstype, " & _
					  "	   NumberOFAns " & _
					 "FROM Question " & _
					 "ORDER BY Qid DESC "

			cmSQL = New SqlCommand(strSQL, Sqlcon)
			drSQL = cmSQL.ExecuteReader()

			If drSQL.Read() Then
				Dim tempqid As String

				' Populate form with the data
				tempqid = drSQL.Item("Qid").ToString()

				txtQid.Text = Val(tempqid) + 1
				'strID = drSQL.Item("QuestionName").ToString()
				' FindItemByID(txtqid, strID)
				'strID = drSQL.Item("NumberOFAnswers").ToString()
				'FindItemByID(ComboBox1, strID)
			End If
			' Close and Clean up objects
			drSQL.Close()
			Sqlcon.Close()
			cmSQL.Dispose()
			Sqlcon.Dispose()
		Catch e As SqlException
			MsgBox(e.Message, MsgBoxStyle.Critical, "SQL Error")
		Catch e As Exception
			MsgBox(e.Message, MsgBoxStyle.Critical, "General Error")
		End Try
	End Sub
	Private Sub PopulateQuestiontList()
		' This procedure populates the list box on the
		' form with a list of available products from the
		' Northwind database.
		'Dim cnSQL As SqlConnection
		Dim cmSQL As New SqlCommand
		Dim drSQL As SqlDataReader
		Dim strSQL As String
		Dim objListItem As ListItem
		Try
			If Sqlcon.State = ConnectionState.Open Then
				Sqlcon.Close()
			End If
			Sqlcon.Open()
			' Build Select statement to query product names from the products
			' table.
			strSQL = "SELECT QuestionName,Qid FROM question"


			cmSQL = New SqlCommand(strSQL, Sqlcon)
			drSQL = cmSQL.ExecuteReader()


			' Loop through the result set using the datareader class.  
			' The datareader is used here because all that is needed 
			' is a forward only cursor which is more efficient.			 
			'Do While drSQL.Read()
			'	objListItem = New ListItem(drSQL.Item("QuestionName").ToString(), _
			'							   CInt(drSQL.Item("Qid")))
			'	lbquestions.Items.Add(objListItem)
			'Loop

			'If lbquestions.Items.Count > 0 Then
			'	lbquestions.SetSelected(0, True)
			'End If
			drSQL.Close()
			Sqlcon.Close()
			cmSQL.Dispose()
			Sqlcon.Dispose()

		Catch e As SqlException
			MsgBox(e.Message, MsgBoxStyle.Critical, "SQL Error")

		Catch e As Exception
			MsgBox(e.Message, MsgBoxStyle.Critical, "General Error")
		End Try
	End Sub
	Private Function ConvertDBNull(ByVal obj As Object) As Object
		If obj Is System.DBNull.Value Then
			Return ""
		Else
			Return obj
		End If
	End Function

	Private Function IncreaseQid()
		Questionid = Questionid + 1
		txtQid.Text = Questionid
	End Function
	Private Function checkqid()
		Dim sSql As String
		Dim questionid As Integer
		Dim sqlcommand As New SqlCommand
		sSql = "Select Qid,NumberOfAnswers from Question"
		sqlcommand.Connection = Sqlcon
		sqlcommand.CommandText = sSql
		sqlcommand.CommandType = CommandType.Text
		Try
			sqlcommand.ExecuteNonQuery()
		Catch ex As Exception
			'MsgBox(ex.ToString)
			'MsgBox(ex)
		End Try
	End Function

	Private Function listboxtotextbox()
		txtAnsplain.Text = lbdisplay.SelectedItem
	End Function
	Private Function insertquery()
		Dim Question, Answer1, Answer2, Answer3, Qtype, Answer4, Answer5, Answer6, Answer7 As String
		Dim NumberofAnswers, QuestionId As Integer
		Question = Trim(txtQuestion.Text)
		Answer1 = Trim(txtAnsplain.Text)
		Answer2 = Trim(txtans2.Text)
		Answer3 = Trim(txtans3.Text)
		Answer4 = Trim(txtans4.Text)
		Answer5 = Trim(txtans5.Text)
		Answer6 = Trim(txtans6.Text)
		Answer7 = Trim(txtans7.Text)
		Qtype = cbxQtype.SelectedItem
		NumberofAnswers = cbxNoofAns.SelectedItem
		Dim DA As New SqlDataAdapter
		Dim DS As New DataSet
		selAns = NumberofAnswers
		Dim sSql As String
		Dim sqlcommand As New SqlCommand
		Dim insrec As String

		sSql = "insert into Question(QuestionId,Question,NumberofAnswers,Qtype,Answer1,Answer2,Answer3,Answer4,Answer5,Answer6,Answer7) values(" & QuestionId & ",'" & Question & "','" & NumberofAnswers & "','" & Qtype & "','" & Answer1 & "','" & Answer2 & "','" & Answer3 & "','" & Answer4 & "','" & Answer5 & "','" & Answer6 & "','" & Answer7 & "')"
		sqlcommand.Connection = Sqlcon
		sqlcommand.CommandText = sSql
		sqlcommand.CommandType = CommandType.Text
		Try
			insrec = sqlcommand.ExecuteNonQuery()
			MsgBox("The newly inserted record is " & insrec.ToString)
		Catch ex As Exception
			'MsgBox(ex)
		End Try
		ShowMsg("Answer(s) Added", True)
		ClearAllFields()
		txtAnsplain.Focus()
	End Function
	Private Function ClearAllFields()
		txtQuestion.Text = ""
		txtAnsplain.Text = ""
		txtans2.Text = ""
		txtans3.Text = ""
		txtans4.Text = ""
		txtans5.Text = ""
		txtans6.Text = ""
		txtans7.Text = ""
		cbxQtype.SelectedItem = Nothing
		cbxNoofAns.SelectedItem = 0
	End Function
	Private Function removefromlistbox()
		lbdisplay.Items.RemoveAt(4)
	End Function
	Private Function initCombox()
		For i As Integer = 1 To 7
			cbxNoofAns.Items.Add(i)
		Next
		cbxQtype.Items.Add("Text")
		cbxQtype.Items.Add("Image")
	End Function
	Private Sub ClearAll()
		txtQuestion.Text = ""
		txtAnsplain.Text = ""
		cbxQtype.Text = ""
		cbxNoofAns.Text = ""
	End Sub
	Private Function VerifyInput() As Boolean
		If txtQuestion.Text = "" Then
			ShowMsg("Please enter the Question!", False)
			Return False
		End If
		If txtAnsplain.Text = "" Then
			ShowMsg("Please enter the Answer!", False)
			Return False
		End If
		If Me.cbxQtype.SelectedItem = Nothing Then
			ShowMsg("Please enter the QuestionType!", False)
			Return False
		End If
		If Me.cbxNoofAns.SelectedItem = 0 Then
			ShowMsg("Please Enter No. Of Answers you want to Set!", False)
			Return False
		End If
		Return True
	End Function
	Private Sub ShowMsg(ByVal strMsg As String, ByVal blnNonerror As Boolean)
		If blnNonerror = True Then
			lblStatus.ForeColor = System.Drawing.Color.Green
		Else
			lblStatus.ForeColor = System.Drawing.Color.Red
		End If
		lblStatus.Text = strMsg
	End Sub

	Private Sub cbxNoofAns_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
		selAns = cbxNoofAns.SelectedItem
	End Sub

	Private Sub displayansinfo()
		Try
			txtAnsplain.Text = ConvertDBNull(dt.Rows(lbdisplay.Items.Add(0)))

		Catch ex As Exception

		End Try
	End Sub

	Private Sub btnAddQuestion_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddQuestion.Click
		Dim Question, Answer1, Answer2, Answer3, Qtype, Answer4, Answer5, Answer6, Answer7 As String
		Dim NumberofAnswers, QuestionId As Integer
		Dim sSql As String
		Dim Sqlcon As New SqlConnection(connString)

		Dim sqlcommand As New SqlCommand
		Dim insrec As String
		Question = Trim(txtQuestion.Text)
		Qtype = cbxQtype.SelectedItem
		NumberofAnswers = cbxNoofAns.SelectedItem
		Sqlcon.Open()
		insertans()
		sSql = "insert into Question(QuestionName,NumberofAns,Anstype) values('" & Question & "','" & NumberofAnswers & "','" & Qtype & "')"
		sqlcommand.Connection = Sqlcon
		sqlcommand.CommandText = sSql
		sqlcommand.CommandType = CommandType.Text
		Try
			insrec = sqlcommand.ExecuteNonQuery()
			MsgBox("The newly inserted record is " & insrec.ToString)
		Catch ex As Exception
			'MsgBox(ex)
		End Try
		ShowMsg("Question & Answer(s) Added", True)
		txtQuestion.Focus()
		btnAddQuestion.Visible = False
	End Sub
	Private Function insertans()
		Dim Answer1, Answer2, Answer3, Anstype, Answer4, Answer5, Answer6, Answer7 As String
		Dim NumberofAns, Qid As Integer
		Dim sSql As String
		Dim Sqlcon As New SqlConnection(connString)
		Sqlcon.Open()
		Dim sqlcommand As New SqlCommand
		Dim insrec As String
		Anstype = cbxQtype.SelectedItem
		NumberofAns = cbxNoofAns.SelectedItem
		Answer1 = txtAnsplain.Text
		Answer2 = txtans2.Text
		Answer3 = txtans3.Text
		Answer4 = txtans4.Text
		Answer5 = txtans5.Text
		Answer6 = txtans6.Text
		Answer7 = txtans7.Text
		Dim selanscbx As Integer
		selanscbx = cbxNoofAns.SelectedItem
		If cbxansid < selanscbx Then
			If cbxQtype.SelectedItem = "Text" Then
				sSql = "insert into answer(Qid,NumberOFAns,Anstype,Answer1,Answer2,Answer3,Answer4,Answer5,Answer6,Answer7) values('" & txtQid.Text & "','" & NumberofAns & "','" & Anstype & "','" & Answer1 & "','" & Answer2 & "','" & Answer3 & "','" & Answer4 & "','" & Answer5 & "','" & Answer6 & "','" & Answer7 & "')"
				sqlcommand.Connection = Sqlcon
				sqlcommand.CommandText = sSql
				sqlcommand.CommandType = CommandType.Text
				Try
					insrec = sqlcommand.ExecuteNonQuery()
				Catch ex As Exception
					'MsgBox(ex)
				End Try
				'ElseIf cbxQtype.SelectedItem = "Image" Then

				txtQuestion.Focus()
			End If
			cbxansid = cbxansid + 1
		End If

	End Function
	Private Sub cbxQtype_SelectedValueChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxQtype.SelectedValueChanged
		Dim Sqlcon As New SqlConnection(connString)

		If cbxQtype.SelectedItem = "Image" Then
			lbdisplay.Items.Clear()
			pbimage.Visible = True
			lblbrowspic.Visible = True
			btnimage.Visible = True
			btnAddQuestion.Visible = True
			lblansenter.Visible = False
			txtAnsplain.Visible = False
			btnAddAns.Visible = False
			btnimage.Visible = True
			btnimgsave.Visible = True
			txtans2.Visible = False
			txtans3.Visible = False
			txtans4.Visible = False
			txtans5.Visible = False
			txtans6.Visible = False
			txtans7.Visible = False
			btnimgsave_Click(sender, e)
		ElseIf cbxQtype.SelectedItem = "Text" Then
			cbxQtype.SelectedItem = "Text"
			pbimage.Visible = False
			lblbrowspic.Visible = False
			btnimage.Visible = False
			btnAddQuestion.Visible = True
			lblansenter.Visible = True
			txtAnsplain.Visible = True
			txtans2.Visible = True
			txtans3.Visible = True
			txtans4.Visible = True
			txtans5.Visible = True
			txtans6.Visible = True
			txtans7.Visible = True
			btnAddAns.Visible = True
			btnimage.Visible = False
		End If
	End Sub

	Private Sub lbdisplay_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbdisplay.Click
		listboxtotextbox()
	End Sub


	Private Sub btnimage_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimage.Click
		With OpenFileDialog1
			.InitialDirectory = "C:\"
			.Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
			.FilterIndex = 2
		End With
		If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
			With pbimage
				.Image = Image.FromFile(OpenFileDialog1.FileName)
				.SizeMode = PictureBoxSizeMode.StretchImage
				.BorderStyle = BorderStyle.FixedSingle
			End With
			lblfilepath.Text = OpenFileDialog1.FileName
		End If
	End Sub

	Private Sub btnimgsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimgsave.Click
		Dim Sqlcon As New SqlConnection(connString)

		If Sqlcon.State = ConnectionState.Open Then
			Sqlcon.Close()
		End If
		Sqlcon.Open()
		Dim selanscbx As Integer
		selanscbx = cbxNoofAns.SelectedItem
		If cbxansid < selanscbx Then
			Dim arrFilename() As String = Split(lblfilepath.Text, "\")
			arrFilename.Reverse(arrFilename)
			Dim ms As New IO.MemoryStream
			pbimage.Image.Save(ms, pbimage.Image.RawFormat)
			Dim arrImage() As Byte = ms.GetBuffer
			Dim strSQL As String = _
					   "INSERT INTO Picture (Filename, Picture1)" & _
					   "VALUES (@Filename, @Picture1)"
			Dim cmd As New SqlCommand(strSQL, Sqlcon)
			With cmd
				.Parameters.Add(New SqlParameter("@Filename", _
							SqlDbType.NVarChar, 50)).Value = arrFilename(0)
				.Parameters.Add(New SqlParameter("@Picture1", _
					SqlDbType.Image)).Value = arrImage
			End With

			cmd.ExecuteNonQuery()
			Sqlcon.Close()
			MessageBox.Show(arrFilename(0) & " saved to the database.", _
						"Image Save Status", MessageBoxButtons.OK, _
						MessageBoxIcon.Information)

			cbxansid = cbxansid + 1
			pbimage.Image = Nothing
			ShowMsg("Going to Next Answer.....", False)
		Else
			MsgBox("Sorry u have Choosen  " & cbxansid & "  answers for updation!")
			pbimage.Image = Nothing

			ShowMsg("Image Answers Added ", True)
		End If

	End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: how to get the answers sequentially

#2 nirmala82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-September 08

Re: how to get the answers sequentially

Posted 27 October 2008 - 10:12 PM

hi frends actually my task is to create the question and answer form,in which question shld be retrived from database for each time the user enters new questions,then type the question,then if the anstype is image or text ,if the user chooses image means the picturebox with browse option and save option should come,and if it is text add ans with textbox should come,then one more important thing is the user has to choose the number of answers,if the answers for the quesiton is 2,then with only one text box answers should be entered in sequence, ie.,first answer then enter key the answer shld be listed on the listbox,then shld come to the textbox box againt o get the sec ans,after geting the sec ans,the ans should get inserted in the list box,suppose if the user want to delete the answer means click the particular ans in the listbox and delete that, my database design is question with qid,numberofans,anstype,questionname, and answe with ansid,quesitonid,numberofanswers,anstype,answer1,answe2,answer3,answer4,answer7,
answer6,answer7,and the same for picture with picid,quesitonname,numberofanswer,anstype,

The conditions is:

If the user chooses anstype as text means,the answers shld get into the answer table with the number of answers,suppose if the user chooses two answers means,after giving two answers,the user should be prompted with u have choosen only two answers,ur limit finished cannot enter third answer,if the user chooses anstype as picture means,the answers should be included in picutere table,like the same as of answer,depeds on the number of pictures choosing the user shld enter the pictures,can anyone help me to handle this,i m totaly new to vb.net,pls if anyone helping,kindly appreciatable...thanks....but anyhow i have did someofthe coding but not working good....

Public Class frmSetQuestions
    Inherits System.Windows.Forms.Form
    Dim dt As DataTable
    Dim Questionid As Integer = 0
    Public cbxansid As Integer = 0
    Dim Sqlcon As New SqlConnection(connString)

Public selAns As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lbquestions.Visible = False
        cbxQtype.SelectedItem = "Text"
        pbimage.Visible = False
        lblbrowspic.Visible = False
        btnimage.Visible = False
        btnAddQuestion.Visible = True
        lblansenter.Visible = True
        txtAnsplain.Visible = True
        txtans2.Visible = True
        txtans3.Visible = True
        txtans4.Visible = True
        txtans5.Visible = True
        txtans6.Visible = True
        txtans7.Visible = True
        btnAddAns.Visible = True
        btnimage.Visible = False
        pbimage.Visible = False
        lblbrowspic.Visible = False
        btnimage.Visible = False
        btnimgsave.Visible = False
        txtQid.Text = Questionid
        
        PopulateQuestiontList()
        PopulateForm()
        initCombox()
        'Shell("C:\WINDOWS\system32\calc.exe", AppWinStyle.MaximizedFocus)
    End Sub
    Private Sub PopulateForm()
        Dim Sqlcon As New SqlConnection(connString)
        Dim cmSQL As SqlCommand
        Dim drSQL As SqlDataReader
        Dim strSQL As String
        Dim objListItem As ListItem
        Dim strID As String
        If Sqlcon.State = ConnectionState.Open Then
            Sqlcon.Close()
        End If
        Sqlcon.Open()
        Try

            ' Get Primary Key from Listbox
            objListItem = CType(lbquestions.SelectedItem, ListItem)
            ' Build Select statement to query product information from the products
            ' table 
            strSQL = "SELECT Qid, " & _
                     "       QuestionName, " & _
                     "       Anstype, " & _
                      "       NumberOFAns " & _
                     "FROM Question " & _
                     "ORDER BY Qid DESC "

            cmSQL = New SqlCommand(strSQL, Sqlcon)
            drSQL = cmSQL.ExecuteReader()

            If drSQL.Read() Then
                Dim tempqid As String

                ' Populate form with the data
                tempqid = drSQL.Item("Qid").ToString()

                txtQid.Text = Val(tempqid) + 1
                'strID = drSQL.Item("QuestionName").ToString()
                ' FindItemByID(txtqid, strID)
                'strID = drSQL.Item("NumberOFAnswers").ToString()
                'FindItemByID(ComboBox1, strID)
            End If
            ' Close and Clean up objects
            drSQL.Close()
            Sqlcon.Close()
            cmSQL.Dispose()
            Sqlcon.Dispose()
        Catch e As SqlException
            MsgBox(e.Message, MsgBoxStyle.Critical, "SQL Error")
        Catch e As Exception
            MsgBox(e.Message, MsgBoxStyle.Critical, "General Error")
        End Try
    End Sub
    Private Sub PopulateQuestiontList()
        ' This procedure populates the list box on the
        ' form with a list of available products from the
        ' Northwind database.
        'Dim cnSQL As SqlConnection
        Dim cmSQL As New SqlCommand
        Dim drSQL As SqlDataReader
        Dim strSQL As String
        Dim objListItem As ListItem
        Try
            If Sqlcon.State = ConnectionState.Open Then
                Sqlcon.Close()
            End If
            Sqlcon.Open()
            ' Build Select statement to query product names from the products
            ' table.
            strSQL = "SELECT QuestionName,Qid FROM question"


            cmSQL = New SqlCommand(strSQL, Sqlcon)
            drSQL = cmSQL.ExecuteReader()


            ' Loop through the result set using the datareader class.  
            ' The datareader is used here because all that is needed 
            ' is a forward only cursor which is more efficient.             
            'Do While drSQL.Read()
            '    objListItem = New ListItem(drSQL.Item("QuestionName").ToString(), _
            '                               CInt(drSQL.Item("Qid")))
            '    lbquestions.Items.Add(objListItem)
            'Loop

            'If lbquestions.Items.Count > 0 Then
            '    lbquestions.SetSelected(0, True)
            'End If
            drSQL.Close()
            Sqlcon.Close()
            cmSQL.Dispose()
            Sqlcon.Dispose()

        Catch e As SqlException
            MsgBox(e.Message, MsgBoxStyle.Critical, "SQL Error")

        Catch e As Exception
            MsgBox(e.Message, MsgBoxStyle.Critical, "General Error")
        End Try
    End Sub
    Private Function ConvertDBNull(ByVal obj As Object) As Object
        If obj Is System.DBNull.Value Then
            Return ""
        Else
            Return obj
        End If
    End Function

    Private Function IncreaseQid()
        Questionid = Questionid + 1
        txtQid.Text = Questionid
    End Function
    Private Function checkqid()
        Dim sSql As String
        Dim questionid As Integer
        Dim sqlcommand As New SqlCommand
        sSql = "Select Qid,NumberOfAnswers from Question"
        sqlcommand.Connection = Sqlcon
        sqlcommand.CommandText = sSql
        sqlcommand.CommandType = CommandType.Text
        Try
            sqlcommand.ExecuteNonQuery()
        Catch ex As Exception
            'MsgBox(ex.ToString)
            'MsgBox(ex)
        End Try
    End Function

    Private Function listboxtotextbox()
        txtAnsplain.Text = lbdisplay.SelectedItem
    End Function
    Private Function insertquery()
        Dim Question, Answer1, Answer2, Answer3, Qtype, Answer4, Answer5, Answer6, Answer7 As String
        Dim NumberofAnswers, QuestionId As Integer
        Question = Trim(txtQuestion.Text)
        Answer1 = Trim(txtAnsplain.Text)
        Answer2 = Trim(txtans2.Text)
        Answer3 = Trim(txtans3.Text)
        Answer4 = Trim(txtans4.Text)
        Answer5 = Trim(txtans5.Text)
        Answer6 = Trim(txtans6.Text)
        Answer7 = Trim(txtans7.Text)
        Qtype = cbxQtype.SelectedItem
        NumberofAnswers = cbxNoofAns.SelectedItem
        Dim DA As New SqlDataAdapter
        Dim DS As New DataSet
        selAns = NumberofAnswers
        Dim sSql As String
        Dim sqlcommand As New SqlCommand
        Dim insrec As String

        sSql = "insert into Question& #40;QuestionId,Question,NumberofAnswers,Qtype,Answer1,Answer2,Answer3,Answer4,An
swer5,Answer6,Answer7) values(" & QuestionId & ",'" & Question & "','" & NumberofAnswers & "','" & Qtype & "','" & Answer1 & "','" & Answer2 & "','" & Answer3 & "','" & Answer4 & "','" & Answer5 & "','" & Answer6 & "','" & Answer7 & "')"
        sqlcommand.Connection = Sqlcon
        sqlcommand.CommandText = sSql
        sqlcommand.CommandType = CommandType.Text
        Try
            insrec = sqlcommand.ExecuteNonQuery()
            MsgBox("The newly inserted record is " & insrec.ToString)
        Catch ex As Exception
            'MsgBox(ex)
        End Try
        ShowMsg("Answer(s) Added", True)
        ClearAllFields()
        txtAnsplain.Focus()
    End Function
    Private Function ClearAllFields()
        txtQuestion.Text = ""
        txtAnsplain.Text = ""
        txtans2.Text = ""
        txtans3.Text = ""
        txtans4.Text = ""
        txtans5.Text = ""
        txtans6.Text = ""
        txtans7.Text = ""
        cbxQtype.SelectedItem = Nothing
        cbxNoofAns.SelectedItem = 0
    End Function
    Private Function removefromlistbox()
        lbdisplay.Items.RemoveAt(4)
    End Function
    Private Function initCombox()
        For i As Integer = 1 To 7
            cbxNoofAns.Items.Add(i)
        Next
        cbxQtype.Items.Add("Text")
        cbxQtype.Items.Add("Image")
    End Function
    Private Sub ClearAll()
        txtQuestion.Text = ""
        txtAnsplain.Text = ""
        cbxQtype.Text = ""
        cbxNoofAns.Text = ""
    End Sub
    Private Function VerifyInput() As Boolean
        If txtQuestion.Text = "" Then
            ShowMsg("Please enter the Question!", False)
            Return False
        End If
        If txtAnsplain.Text = "" Then
            ShowMsg("Please enter the Answer!", False)
            Return False
        End If
        If Me.cbxQtype.SelectedItem = Nothing Then
            ShowMsg("Please enter the QuestionType!", False)
            Return False
        End If
        If Me.cbxNoofAns.SelectedItem = 0 Then
            ShowMsg("Please Enter No. Of Answers you want to Set!", False)
            Return False
        End If
        Return True
    End Function
    Private Sub ShowMsg(ByVal strMsg As String, ByVal blnNonerror As Boolean)
        If blnNonerror = True Then
            lblStatus.ForeColor = System.Drawing.Color.Green
        Else
            lblStatus.ForeColor = System.Drawing.Color.Red
        End If
        lblStatus.Text = strMsg
    End Sub

    Private Sub cbxNoofAns_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        selAns = cbxNoofAns.SelectedItem
    End Sub

    Private Sub displayansinfo()
        Try
            txtAnsplain.Text = ConvertDBNull(dt.Rows(lbdisplay.Items.Add(0)))

        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnAddQuestion_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddQuestion.Click
        Dim Question, Answer1, Answer2, Answer3, Qtype, Answer4, Answer5, Answer6, Answer7 As String
        Dim NumberofAnswers, QuestionId As Integer
        Dim sSql As String
        Dim Sqlcon As New SqlConnection(connString)

        Dim sqlcommand As New SqlCommand
        Dim insrec As String
        Question = Trim(txtQuestion.Text)
        Qtype = cbxQtype.SelectedItem
        NumberofAnswers = cbxNoofAns.SelectedItem
        Sqlcon.Open()
        insertans()
        sSql = "insert into Question(QuestionName,NumberofAns,Anstype) values('" & Question & "','" & NumberofAnswers & "','" & Qtype & "')"
        sqlcommand.Connection = Sqlcon
        sqlcommand.CommandText = sSql
        sqlcommand.CommandType = CommandType.Text
        Try
            insrec = sqlcommand.ExecuteNonQuery()
            MsgBox("The newly inserted record is " & insrec.ToString)
        Catch ex As Exception
            'MsgBox(ex)
        End Try
        ShowMsg("Question & Answer(s) Added", True)
        txtQuestion.Focus()
        btnAddQuestion.Visible = False
    End Sub
    Private Function insertans()
        Dim Answer1, Answer2, Answer3, Anstype, Answer4, Answer5, Answer6, Answer7 As String
        Dim NumberofAns, Qid As Integer
        Dim sSql As String
        Dim Sqlcon As New SqlConnection(connString)
        Sqlcon.Open()
        Dim sqlcommand As New SqlCommand
        Dim insrec As String
        Anstype = cbxQtype.SelectedItem
        NumberofAns = cbxNoofAns.SelectedItem
        Answer1 = txtAnsplain.Text
        Answer2 = txtans2.Text
        Answer3 = txtans3.Text
        Answer4 = txtans4.Text
        Answer5 = txtans5.Text
        Answer6 = txtans6.Text
        Answer7 = txtans7.Text
        Dim selanscbx As Integer
        selanscbx = cbxNoofAns.SelectedItem
        If cbxansid < selanscbx Then
            If cbxQtype.SelectedItem = "Text" Then
                sSql = "insert into answer& #40;Qid,NumberOFAns,Anstype,Answer1,Answer2,Answer3,Answer4,Answer5,Answer6,Answ
er7) values('" & txtQid.Text & "','" & NumberofAns & "','" & Anstype & "','" & Answer1 & "','" & Answer2 & "','" & Answer3 & "','" & Answer4 & "','" & Answer5 & "','" & Answer6 & "','" & Answer7 & "')"
                sqlcommand.Connection = Sqlcon
                sqlcommand.CommandText = sSql
                sqlcommand.CommandType = CommandType.Text
                Try
                    insrec = sqlcommand.ExecuteNonQuery()
                Catch ex As Exception
                    'MsgBox(ex)
                End Try
                'ElseIf cbxQtype.SelectedItem = "Image" Then

                txtQuestion.Focus()
            End If
            cbxansid = cbxansid + 1
        End If

    End Function
    Private Sub cbxQtype_SelectedValueChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxQtype.SelectedValueChanged
        Dim Sqlcon As New SqlConnection(connString)

        If cbxQtype.SelectedItem = "Image" Then
            lbdisplay.Items.Clear()
            pbimage.Visible = True
            lblbrowspic.Visible = True
            btnimage.Visible = True
            btnAddQuestion.Visible = True
            lblansenter.Visible = False
            txtAnsplain.Visible = False
            btnAddAns.Visible = False
            btnimage.Visible = True
            btnimgsave.Visible = True
            txtans2.Visible = False
            txtans3.Visible = False
            txtans4.Visible = False
            txtans5.Visible = False
            txtans6.Visible = False
            txtans7.Visible = False
            btnimgsave_Click(sender, e)
        ElseIf cbxQtype.SelectedItem = "Text" Then
            cbxQtype.SelectedItem = "Text"
            pbimage.Visible = False
            lblbrowspic.Visible = False
            btnimage.Visible = False
            btnAddQuestion.Visible = True
            lblansenter.Visible = True
            txtAnsplain.Visible = True
            txtans2.Visible = True
            txtans3.Visible = True
            txtans4.Visible = True
            txtans5.Visible = True
            txtans6.Visible = True
            txtans7.Visible = True
            btnAddAns.Visible = True
            btnimage.Visible = False
        End If
    End Sub

    Private Sub lbdisplay_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbdisplay.Click
        listboxtotextbox()
    End Sub


    Private Sub btnimage_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimage.Click
        With OpenFileDialog1
            .InitialDirectory = "C:\"
            .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
            .FilterIndex = 2
        End With
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            With pbimage
                .Image = Image.FromFile(OpenFileDialog1.FileName)
                .SizeMode = PictureBoxSizeMode.StretchImage
                .BorderStyle = BorderStyle.FixedSingle
            End With
            lblfilepath.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub btnimgsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimgsave.Click
        Dim Sqlcon As New SqlConnection(connString)

        If Sqlcon.State = ConnectionState.Open Then
            Sqlcon.Close()
        End If
        Sqlcon.Open()
        Dim selanscbx As Integer
        selanscbx = cbxNoofAns.SelectedItem
        If cbxansid < selanscbx Then
            Dim arrFilename() As String = Split(lblfilepath.Text, "\")
            arrFilename.Reverse(arrFilename)
            Dim ms As New IO.MemoryStream
            pbimage.Image.Save(ms, pbimage.Image.RawFormat)
            Dim arrImage() As Byte = ms.GetBuffer
            Dim strSQL As String = _
                       "INSERT INTO Picture (Filename, Picture1)" & _
                       "VALUES (@Filename, @Picture1)"
            Dim cmd As New SqlCommand(strSQL, Sqlcon)
            With cmd
                .Parameters.Add(New SqlParameter("@Filename", _
                            SqlDbType.NVarChar, 50)).Value = arrFilename(0)
                .Parameters.Add(New SqlParameter("@Picture1", _
                    SqlDbType.Image)).Value = arrImage
            End With

            cmd.ExecuteNonQuery()
            Sqlcon.Close()
            MessageBox.Show(arrFilename(0) & " saved to the database.", _
                        "Image Save Status", MessageBoxButtons.OK, _
                        MessageBoxIcon.Information)

            cbxansid = cbxansid + 1
            pbimage.Image = Nothing
            ShowMsg("Going to Next Answer.....", False)
        Else
            MsgBox("Sorry u have Choosen  " & cbxansid & "  answers for updation!")
            pbimage.Image = Nothing

            ShowMsg("Image Answers Added ", True)
        End If

    End Sub



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: how to get the answers sequentially

Posted 28 October 2008 - 08:24 AM

Please don't create duplicate topics. Merged
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1