How to display result in GridView? - vb.net

NET WinForms.
VB code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Beginning"
Dim a As Integer = 20
Dim b As Integer = 3
Do Until b > a
a & " " & b
a = a - 2
b = b + 1
Loop
Label2.Text = "End"
End Sub
I want to display the result of this row a & " " & b in GridView.
How should I change the code to make this work properly?

I would recommend you store the value into DataTable and bind into the DataGridView
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Beginning"
'Create a new datatable here
Dim dt As New DataTable
dt.Columns.Add("Result")
Dim a As Integer = 20
Dim b As Integer = 3
Do Until b > a
'Create DataRow here and put the value into DataRow
Dim dr As DataRow = dt.NewRow
dr("result") = a.ToString & " " & b.ToString
'a & " " & b
dt.Rows.Add(dr)
a = a - 2
b = b + 1
Loop
'Bind your dt into the GridView
DataGridView.DataSource = dt
Label2.Text = "End"
End Sub

Add DataGridView to your form, and add 2 columns, then next updated code will do that
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Beginning"
' If the DataGridView is not bound to any data source, this code will clear content
DataGridView1.Rows.Clear()
Dim a As Integer = 20
Dim b As Integer = 3
Do Until b > a
'a & " " & b
' add the row to the end of the grid with the Add() method of the Rows collection...
DataGridView1.Rows.Add(New String(){a.ToString(), b.ToString()})
a = a - 2
b = b + 1
Loop
Label2.Text = "End"
End Sub

Related

Visual basic 2010, database – how to fill specific field

I'm creating database connection in Visual Basic. When user click on "borrow book" it transfer chosen data into table used for lent books. It all works alright, but I need last thing. In my database in lent table I have attribute "End date of lent" and I want fill it everytime when user borrow a book with date today + 1 month (for instance, if now is 19/04/2016, end date will be 19/05/2016). I created method "BorrowTime" for this, but I don't know what I should type into and how can I get value of inserted row into "row". And sorry for horrible look of code and form...
Public Class frm_UserView
Dim BooksSource As New BindingSource
Dim BorrowSource As New BindingSource
Dim BooksView As New DataView
Dim BorrowView As New DataView
Dim ds As New DataSet
Dim Borrow As Byte
Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmr_Timer.Start()
Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)
BooksSource = dgd_UserBookView.DataSource
BooksView = CType(BooksSource.List, DataView)
ds = BooksView.DataViewManager.DataSet.Clone
BorrowSource.DataSource = ds
BorrowSource.DataMember = "Books"
BorrowView = CType(BorrowSource.List, DataView)
dgd_User_Borrow_View.DataSource = BorrowSource
End Sub
Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
Borrow = 1
MoveBooks(dgd_UserBookView, BorrowView, Borrow)
dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BorrowSource.MoveLast()
BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
For i = 0 To source.SelectedRows.Count - 1
Dim numberOfRow As Integer
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
If borrow = 1 Then
numberOfRow = 0
BorrowTime(numberOfRow, dgd_User_Borrow_View)
End If
col = col + 1
Next
Next
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
Borrow = 0
MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)
dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BooksSource.MoveLast()
BooksSource.MoveLast()
End Sub
Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
frm_Login.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
Dim countBooks As Integer
Dim countLent As Integer
countBooks = BooksSource.Count
countLent = BorrowSource.Count
lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub
End Class
I need to put code here:
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
My form
Public Class frm_UserView
Dim BooksSource As New BindingSource
Dim BorrowSource As New BindingSource
Dim BooksView As New DataView
Dim BorrowView As New DataView
Dim ds As New DataSet
Dim Borrow As Byte
Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmr_Timer.Start()
Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)
BooksSource = dgd_UserBookView.DataSource
BooksView = CType(BooksSource.List, DataView)
ds = BooksView.DataViewManager.DataSet.Clone
BorrowSource.DataSource = ds
BorrowSource.DataMember = "Books"
BorrowView = CType(BorrowSource.List, DataView)
dgd_User_Borrow_View.DataSource = BorrowSource
End Sub
Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
Borrow = 1
MoveBooks(dgd_UserBookView, BorrowView, Borrow)
dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BorrowSource.MoveLast()
BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
For i = 0 To source.SelectedRows.Count - 1
Dim numberOfRow As Integer
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
If borrow = 1 Then
numberOfRow = 0
BorrowTime(numberOfRow, dgd_User_Borrow_View)
End If
col = col + 1
Next
Next
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
Borrow = 0
MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)
dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BooksSource.MoveLast()
BooksSource.MoveLast()
End Sub
Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
frm_Login.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
Dim countBooks As Integer
Dim countLent As Integer
countBooks = BooksSource.Count
countLent = BorrowSource.Count
lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub
End Class

how to count the number of check boxes checked in visual basic?

Private Sub Btn_Cast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Cast.Click
Dim Counter As Integer = 0
If ChkBox_Barton.CheckState = 1 And ChkBox_Martin.CheckState = 1 And ChkBox_Richards.CheckState = 1 Then
MsgBox("Don't vote for more than 2")
End If
Dim Count_Barton As Integer
Dim Count_Martin As Integer
Dim Count_Richards As Integer
If ChkBox_Barton.Checked Then Count_Barton += 1
If ChkBox_Martin.Checked = 1 Then Count_Martin += 1
If ChkBox_Richards.CheckState = 1 Then Count_Richards += 1
End Sub
Problem is, I'm trying to count it everytime, then let it reset and count again.
Example. I select Barton one time, click vote, then i should be able to select someone new and click vote and it should keep counting.
what can I do?
I need to then display my results. Should I just hold the number in a text or Integer file then display it that way?
I quickly set up your application myself.
Following Code applies to this GUI:
Code:
Public Class VoteCounter
Dim intCountBarton As Integer
Dim intCountMartin As Integer
Dim intCountRichards As Integer
Private Sub ButtonVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonVote.Click
If CheckBoxBarton.CheckState = 1 And CheckBoxMartin.CheckState = 1 And CheckBoxRichards.CheckState = 1 Then
MsgBox("Don't vote for more than 2")
CheckBoxBarton.Checked = False
CheckBoxMartin.Checked = False
CheckBoxRichards.Checked = False
End If
If CheckBoxBarton.Checked Then
intCountBarton += 1
End If
If CheckBoxMartin.Checked Then
intCountMartin = intCountMartin + 1
End If
If CheckBoxRichards.Checked Then
intCountRichards = intCountRichards + 1
End If
CheckBoxBarton.Checked = False
CheckBoxMartin.Checked = False
CheckBoxRichards.Checked = False
End Sub
Private Sub ButtonResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonResult.Click
MsgBox("Barton: " & intCountBarton & vbNewLine & "Martin: " & intCountMartin & vbNewLine & "Richards: " & intCountRichards)
End Sub
Private Sub ButtonReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReset.Click
CheckBoxBarton.Checked = False
CheckBoxMartin.Checked = False
CheckBoxRichards.Checked = False
intCountBarton = 0
intCountMartin = 0
intCountRichards = 0
End Sub
End Class
Dim Count_Barton As Integer = 0
Dim Count_Martin As Integer = 0
Dim Count_Richards As Integer = 0
Private Sub Btn_Cast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Cast.Click
Dim Counter As Integer = 0 'NOT SURE WHAT THIS IS DOING... NOT BEING USED
If ChkBox_Barton.CheckState = 1 And ChkBox_Martin.CheckState = 1 And ChkBox_Richards.CheckState = 1 Then
MsgBox("Don't vote for more than 2")
Else
If ChkBox_Barton.Checked Then Count_Barton += 1
If ChkBox_Martin.Checked = 1 Then Count_Martin += 1
If ChkBox_Richards.CheckState = 1 Then Count_Richards += 1
End If
End Sub

Searching In datagridview in Windows Application VB.NET

I have to filter datagridview using textbox.The code below I am using to fill gridview.getdata function of db class returns the datatable.
I am not using datasource property of gridview instead I am filing gridview using loop.
I can do searching using datasource property and dataview but i have not to fill datagridview directly from datasource property.
Sub griddesgn()
DataGridView1.Columns.Clear()
DataGridView1.Rows.Clear()
DataGridView1.Columns.Add("crime", "crime")
DataGridView1.Columns.Add("actname", "actname")
DataGridView1.Columns.Add("section", "section")
DataGridView1.Columns.Add("description", "description")
End Sub
Private Sub TEST_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
griddesgn()
Dim DBOBJ As New db
Dim DTT As DataTable = DBOBJ.getdata("SELECT crime,actname,section,description from natureofcomplaint_women")
If DTT.Rows.Count > 0 Then
For i As Integer = 0 To DTT.Rows.Count - 1
DataGridView1.Rows.Add()
DataGridView1.Rows(i).Cells("crime").Value = DTT.Rows(i).Item("crime") & ""
DataGridView1.Rows(i).Cells("actname").Value = DTT.Rows(i).Item("actname") & ""
DataGridView1.Rows(i).Cells("section").Value = DTT.Rows(i).Item("section") & ""
DataGridView1.Rows(i).Cells("description").Value = DTT.Rows(i).Item("description") & ""
Next
End If
End Sub
Use the WHERE statement in your SQL Query
"SELECT crime,actname,section,description from natureofcomplaint_women WHERE crime = " & txtSearch.text
(If you want to search on crime.)
Change to your needs.
Just repeat the datagrid fill you used above, but with an altereted SQL query each time you type/press the search button
I still think it's best to bind...
Sub griddesgn()
DataGridView1.Columns.Clear()
DataGridView1.Rows.Clear()
DataGridView1.Columns.Add("crime", "crime")
DataGridView1.Columns.Add("actname", "actname")
DataGridView1.Columns.Add("section", "section")
DataGridView1.Columns.Add("description", "description")
End Sub
Private Sub TEST_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
griddesgn()
Dim DBOBJ As New db
Dim DTT As DataTable = DBOBJ.getdata("SELECT crime,actname,section,description from natureofcomplaint_women")
Dim source1 as New BindingSource()
source1.DataSource = DTT
source1.Filter = "crime = '" & textboxtCrime.text & "'"
DataGrideView1.DataSource = source1
End Sub
Sub griddesgn()
DataGridView1.Columns.Clear()
DataGridView1.Rows.Clear()
DataGridView1.Columns.Add("crime", "crime")
DataGridView1.Columns.Add("actname", "actname")
DataGridView1.Columns.Add("section", "section")
DataGridView1.Columns.Add("description", "description")
End Sub
Private Sub TEST_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
griddesgn()
Dim DBOBJ As New db
Dim DTT As DataTable = DBOBJ.getdata("SELECT crime,actname,section,description from natureofcomplaint_women")
If DTT.Rows.Count > 0 Then
For i As Integer = 0 To DTT.Rows.Count - 1
If (DTT.Rows(i).Item("Crime").Contains(txtCrimeFilter.Text) AND & _
(DTT.Rows(i).Item("actname").Contains(txtActnameFilter.Text) AND & _
(DTT.Rows(i).Item("section").Contains(txtSectionFilter.Text) AND & _
(DTT.Rows(i).Item("description").Contains(txtDescriptionFilter.Text)
DataGridView1.Rows.Add()
DataGridView1.Rows(i).Cells("crime").Value = DTT.Rows(i).Item("crime") & ""
DataGridView1.Rows(i).Cells("actname").Value = DTT.Rows(i).Item("actname") & ""
DataGridView1.Rows(i).Cells("section").Value = DTT.Rows(i).Item("section") & ""
DataGridView1.Rows(i).Cells("description").Value = DTT.Rows(i).Item("description") & ""
End If
Next
End If
End Sub

How would I insert items from textbox into listview?

I have a listview with two columns. I also have a textbox. In the textbox, there are lines with strings that I want to insert into the listview.
Every 1st line will be inserted into the first column and every 2nd line will be inserted into the second column. How would I achieve this.
This is my current code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox3.Text = My.Resources.clsid
Dim ListArr(230) As String
Dim i As Integer = 0
For Each line As String In TextBox3.Lines
ListArr(i) = line(i)
i += 1
For Each litem As String In ListArr
AddLitem(litem, litem)
Next
Next
End Sub
Public Function AddLitem(ByVal Desc As String, ByVal CLSID As String)
Dim litem As ListViewItem
If i = 0 Then
Lstr(0) = Desc
i += 1
ElseIf i = 1 Then
Lstr(1) = Desc
litem = New ListViewItem(Lstr)
ListView1.Items.Add(litem)
ListView1.Items.RemoveAt(ListView1.Items.Count)
End If
Lstr(0) = Desc
'Lstr(1) = CLSID
End Function
Note:
You can us Math.Mod to check if you have to handle the first or second row.
For example
0 mod 2 result 0
1 mod 2 result 1
2 mod 2 result 0
3 mod 2 result 1
I use ListView1.Clear to make sure, i'm refilling an empty ListView.
Example Code. This copies all lines from the TextBox1 to the ListView1.
Separeted by a pipe |.
Delete the IIf(String.IsNullOrEmpty(strView2), "", "|") & if you don't want this separation.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strView1 As String = ""
Dim strView2 As String = ""
For i As Integer = 0 To TextBox1.Lines.Count - 1
If (i Mod 2) Then
strView2 &= IIf(String.IsNullOrEmpty(strView2), "", "|") & TextBox1.Lines(i)
Else
strView1 &= IIf(String.IsNullOrEmpty(strView1), "", "|") & TextBox1.Lines(i)
End If
Next
ListView1.Clear()
ListView1.Items.Add(strView1)
ListView1.Items.Add(strView2)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "cat" & vbCrLf & "street" & vbCrLf & "dog" & vbCrLf & "house" & vbCrLf & "bird" & vbCrLf & "garden"
End Sub
End Class

IndexOutOfRangeException was Unhandled. Cannot find table 0

can anyone please help on how to fix this error? im getting an error "Error:Syntax error(missing operator)in query expression '''ans'." this is my code:
Imports System.Data
Imports System.Data.OleDb
Public Class frmExam : Inherits System.Windows.Forms.Form
Dim ds As New DataSet()
Dim qno() As Integer
Dim pos As Integer
Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
ProcessAnswer()
Dim i, marks As Integer
Dim dr As DataRow
marks = 0
For i = 0 To NOQ - 1
dr = ds.Tables(0).Rows(qno(i))
If Not IsDBNull(dr.Item("ans")) AndAlso dr.Item("CorrectAns") = dr.Item("ans") Then
marks += 1
End If
Next
Try
con.Open()
Dim cmd As New OleDbCommand("insert into stud values(stud.nextval,'" & SubjectCode & "','" & Username & "','" & marks & ")", con)
MsgBox(cmd.CommandText)
cmd.ExecuteNonQuery()
Dim msg As String
msg = "UserName : " & Username & ControlChars.CrLf & _
"Subject : " & SubjectCode & ControlChars.CrLf & _
"Total Questions : " & NOQ & ControlChars.CrLf & _
"Marks : " & marks
MsgBox(msg, "Result")
Catch ex As Exception
MsgBox(ex.Message)
Finally
If con.State <> ConnectionState.Closed Then
con.Close()
End If
End Try
Me.Dispose()
End Sub
Private Sub frmExam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ReDim qno(NOQ)
Try
Dim da As New OleDbDataAdapter("select Question, Ans1, Ans2, Ans3, Ans4, CorrectAns, QType, '' ans from question where SubjectCode = " & SubjectCode, con)
da.Fill(ds, "question")
Catch ex As Exception
MsgBox("Error:" & ex.Message)
Me.Dispose()
End Try
Randomize()
Dim totrows As Integer
totrows = ds.Tables(0).Rows.Count
Dim i, r, j As Integer
Dim present As Boolean
i = 0
Do While i < 5
r = CInt((totrows - 1) * Rnd())
present = False
For j = 0 To i
If r = qno(j) Then
present = True
Exit For
End If
Next
If Not present Then
qno(i) = r
i = i + 1
End If
Loop
pos = 0
DisplayQuestion()
End Sub
Sub DisplayQuestion()
Dim row As DataRow
lblQno.Text = Str(pos + 1) & "/" & NOQ
lblSubcode.Text = "Subject : " & SubjectCode
row = ds.Tables(0).Rows(qno(pos))
txtQuestion.Text = row.Item(0)
txtAns1.Text = row.Item(1)
txtAns2.Text = row.Item(2)
txtAns3.Text = row.Item(3)
txtAns4.Text = row.Item(4)
lblQNo2.Text = Str(pos + 1) & "/" & NOQ
lblSubCode2.Text = "Subject:" & SubjectCode
row = ds.Tables(0).Rows(qno(pos))
txtQuestion2.Text = row.Item(0)
txtAnsTrue.Text = row.Item(1)
txtAnsFalse.Text = row.Item(2)
lblQNo3.Text = Str(pos + 1) & "/" & NOQ
lblSubcode.Text = "Subject : " & SubjectCode
row = ds.Tables(0).Rows(qno(pos))
txtQuestion3.Text = row.Item(1)
txtAns.Text = row.Item(2)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
ProcessAnswer()
If pos < NOQ - 1 Then
pos = pos + 1
DisplayQuestion()
Else
Beep()
End If
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
ProcessAnswer()
If pos > 0 Then
pos = pos - 1
DisplayQuestion()
End If
End Sub
Public Sub ProcessAnswer()
Dim row As DataRow
Dim ans As String = ""
row = ds.Tables(0).Rows(qno(pos))
If rdbtnAns1.Checked Then
ans = "1"
End If
If rdbtnAns2.Checked Then
ans = "2"
End If
If rdbtnAns3.Checked Then
ans = "3"
End If
If rdbtnAns4.Checked Then
ans = "4"
End If
If rdbtnTrue.Checked Then
ans = "1"
End If
If rdbtnFalse.Checked Then
ans = "2"
End If
If txtAns.Text = "" Then
ans = "txtAns"
End If
ds.Tables(0).Rows(qno(pos)).Item("Ans1") = ans
End Sub
Private Sub btnPrev2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev2.Click
ProcessAnswer()
If pos > 0 Then
pos = pos - 1
DisplayQuestion()
End If
End Sub
Private Sub btnNext2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext2.Click
ProcessAnswer()
If pos < NOQ - 1 Then
pos = pos + 1
DisplayQuestion()
Else
Beep()
End If
End Sub
Private Sub btnPrev3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev3.Click
ProcessAnswer()
If pos > 0 Then
pos = pos - 1
DisplayQuestion()
End If
End Sub
Private Sub btnNext3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext3.Click
ProcessAnswer()
If pos < NOQ - 1 Then
pos = pos + 1
DisplayQuestion()
Else
Beep()
End If
End Sub
End Class
this is the highlighted texxt:totrows = ds.Tables(0).Rows.Count
When you want to use an alias for a field expression, Access requires you to use the AS keyword.
This would fail ...
select Question, Ans1, Ans2, Ans3, Ans4, CorrectAns, QType, '' ans from question
This could work ...
select Question, Ans1, Ans2, Ans3, Ans4, CorrectAns, QType, '' AS ans from question
However, I would be uneasy with a field whose name matched the table name. If you can't rename that field, alias the table, and qualify the fields with the table alias.
select q.Question, q.Ans1, q.Ans2, q.Ans3, q.Ans4, q.CorrectAns, q.QType, '' AS ans from question AS q