i am a beginner, i don't how to give command how to display a msgbox that " ID already exist " for a given textbox should i use getfocus if so how????? or is there another way???????/
1. Please try and edit your previous post rather than...Just posting a new one.. 2. use the quote tags properly.. 3. Please clear you question...You want a alert box or a simple error text...
If you are not sure where to give the statement then can you show us your code? Sorry to say, if you had tried everything you would have solved that problem.I do'nt think that you have tried everything.
Code: Imports System.Data Imports System.Data.OleDb Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack = True Then Reset() End If End Sub Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click Dim con As New OleDbConnection Dim com As New OleDbCommand Dim sqlString, STID, STNAME, AGE, ADMNO, FNAME, MNAME, BATCH, ADDRESS, PHNO, MOBNO, EMAIL As String STID = TextBox1.Text STNAME = TextBox2.Text AGE = TextBox3.Text ADMNO = TextBox4.Text FNAME = TextBox5.Text MNAME = TextBox6.Text BATCH = TextBox7.Text ADDRESS = TextBox8.Text PHNO = TextBox9.Text MOBNO = TextBox10.Text EMAIL = TextBox13.Text con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Varsha\My music\Visual Studio 2005\WebSites\PSA\App_Data\student.mdb") con.Open() sqlString = "insert into Table1 values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox13.Text & "')" com = New OleDbCommand(sqlString, con) com.ExecuteNonQuery() MsgBox("Data is successfully entered") con.Close() End Sub
Code: Imports System.Data Imports System.Data.OleDb Partial Class _Default Inherits System.Web.UI.Page Dim con As OleDbConnection 'Only need to create OleDbConnection type reference Dim com As OleDbCommand 'Only need to create OleDbCommand type reference Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack = True Then Reset() End If End Sub Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click Dim sqlString, STID, STNAME, AGE, ADMNO, FNAME, MNAME, BATCH, ADDRESS, PHNO, MOBNO, EMAIL As String STID = TextBox1.Text STNAME = TextBox2.Text AGE = TextBox3.Text ADMNO = TextBox4.Text FNAME = TextBox5.Text MNAME = TextBox6.Text BATCH = TextBox7.Text ADDRESS = TextBox8.Text PHNO = TextBox9.Text MOBNO = TextBox10.Text EMAIL = TextBox13.Text con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Varsha\My music\Visual Studio 2005\WebSites\PSA\App_Data\student.mdb") con.Open() sqlString = "insert into Table1 values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox13.Text & "')" com = New OleDbCommand(sqlString, con) com.ExecuteNonQuery() MsgBox("Data is successfully entered") con.Close() End Sub Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Dim checkID As String = "select id_field from table1 where id_field='" + TextBox1.Text + "'" con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Varsha\My music\Visual Studio 2005\WebSites\PSA\App_Data\student.mdb") con.Open() com = New OleDbCommand(checkID, con) Dim result As SByte = com.ExecuteNonQuery() con.Close() If result > 0 Then MsgBox("ID Already Exists!") ' Code here to disable submit button Else 'code here to enable submit button End If End Sub End Class Don't hesitate to reply if problem persists.
Dim result As SByte = com.ExecuteNonQuery() IM getting this error for that statement Data type mismatch in criteria expression.
oh that was a silly mistake from my side.You can use typecasting: Code: Dim result As SByte =CType(com.ExecuteNonQuery(),SByte) or you use directly declare variable of type Integer Code: Dim result as Integer=com.ExecuteNonQuery() I suggest you to first grasp the basic things.Then go for such topics.
Yes, if your database tables's fields do nat match the data you are submitting then you will definitely get such an error. Can you post your table's column in.
<Columns> <asp:BoundField DataField="STUDENT_ID" HeaderText="STUDENT_ID" SortExpression="STUDENT_ID" /> <asp:BoundField DataField="STUDENT_NAME" HeaderText="STUDENT_NAME" SortExpression="STUDENT_NAME" /> <asp:BoundField DataField="AGE" HeaderText="AGE" SortExpression="AGE" /> <asp:BoundField DataField="ADMISSION_NUMBER" HeaderText="OCCUPATION" SortExpression="ADMISSION_NUMBER" /> <asp:BoundField DataField="FATHER_NAME" HeaderText="FATHER_NAME" SortExpression="FATHER_NAME" /> <asp:BoundField DataField="MOTHER_NAME" HeaderText="MOTHER_NAME" SortExpression="MOTHER_NAME" /> <asp:BoundField DataField="YEAR_OF_THE_BATCH" HeaderText="YEAR_OF_THE_BATCH" SortExpression="YEAR_OF_THE_BATCH" /> <asp:BoundField DataField="RESIDENTIAL_ADDRESS" HeaderText="RESIDENTIAL_ADDRESS" SortExpression="RESIDENTIAL_ADDRESS" /> <asp:BoundField DataField="PHONE_NO" HeaderText="PHONE_NO" SortExpression="PHONE_NO" /> <asp:BoundField DataField="MOBILE_NO" HeaderText="MOBILE_NO" SortExpression="MOBILE_NO" /> <asp:BoundField DataField="EMAIL_ID" HeaderText="EMAIL_ID" SortExpression="EMAIL_ID" /> </Columns>
I am asking about column present in your table(the table on which your insert query is working). Please post your table columns with there respective data type.
<asp:Parameter Name="STUDENT_NAME" Type="String" /> <asp:Parameter Name="AGE" Type="Int32" /> <asp:Parameter Name="ADMISSION_NUMBER" Type="String" /> <asp:Parameter Name="FATHER_NAME" Type="String" /> <asp:Parameter Name="MOTHER_NAME" Type="String" /> <asp:Parameter Name="YEAR_OF_THE_BATCH" Type="Int32" /> <asp:Parameter Name="RESIDENTIAL_ADDRESS" Type="String" /> <asp:Parameter Name="PHONE_NO" Type="Int32" /> <asp:Parameter Name="MOBILE_NO" Type="Double" /> <asp:Parameter Name="EMAIL_ID" Type="String" /> <asp:Parameter Name="STUDENT_ID" Type="Double" />
Exactly, i was expecting the same. Data type mismatch . Now this will be your insert query: Code: sqlString = "insert into Table1 values('" & TextBox1.Text & "'," & TextBox2.Text & ",'" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "'," & TextBox6.Text & ",'" & TextBox7.Text & "'," & TextBox8.Text & "," & TextBox9.Text & ",'" & TextBox10.Text & "'," & TextBox13.Text & ")" Remember, for string datatype use single quotes for the string and for numeric datatype do not use any single quotes. Is the issue resolved now ?