0

I had a project running perfectly i.e. entering values in database linked by code. But in the other project I am implementing it shows values correctly submitted, gives no error while running but values are not entered in database.

Private Sub frmAddresume_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load cn.ConnectionString = "Data Source=ROHAN-PC\SQLEXPRESS;initial catalog=Libeasy;Integrated Security=true" DateTimePicker1.Value = DateTime.Now.ToShortDateString() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click If (TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "") Then cn.Open() cmd.Connection = cn cmd.CommandText = "insert into StudResume values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DateTimePicker1.Value.ToShortDateString() + "'," + TextBox3.Text + ")" cmd.Dispose() cn.Close() MsgBox("Details saved Successfully", MsgBoxStyle.Information, "Done") TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" DateTimePicker1.Value = Now TextBox1.Focus() Else MsgBox("Please Enter Complete Details", MsgBoxStyle.Critical, "Error") End If End Sub 

3 Answers 3

1

You've missed the executenonquery(),so the query you have provided is niether executed.replace the below code and eeverything will be working.

cmd.Connection = cn cmd.CommandText = "insert into StudResume values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DateTimePicker1.Value.ToShortDateString() + "'," + TextBox3.Text + ")" cmd.ExecuteNonQuery() cmd.Dispose() cn.Close() 

Ie Add cmd.ExecuteNonQuery() after providing the commandtext.

Sign up to request clarification or add additional context in comments.

3 Comments

check the query.it may be issue with the query parameters.
actually i have a working block of code of another project same like the above though that one was working
ohh anyway please check what will be the commandtext you are executing while you get n exception.
0

change your button3 code as the following, it will give u an error message in msgbox, post the error message here so may we can help,

 If (TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "") Then Try cn.Open() cmd.Connection = cn cmd.CommandText = "insert into StudResume values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DateTimePicker1.Value.ToShortDateString() + "'," + TextBox3.Text + ")" cmd.Dispose() cn.Close() MsgBox("Details saved Successfully", MsgBoxStyle.Information, "Done") TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" DateTimePicker1.Value = Now TextBox1.Focus() Catch ex As Exception MsgBox(ex.Message) Finally End Try Else MsgBox("Please Enter Complete Details", MsgBoxStyle.Critical, "Error") End If 

1 Comment

I just found that may the problem is because of the (" + TextBox3.Text + ") if the value in texbox3 is string, so there should be the single quotation mark '"+TextBox3.text+"'
0

Try this way cn =new SqlConnection("Data Source=ROHAN-PC\SQLEXPRESS;initial catalog=Libeasy;Integrated Security=true")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.