Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

i made one window base application in VS2008 (VB.net).
i made one simple code where i can insert data.and after inserting i can see that data on grid.when i am inserting data insert query runs perfect.after that for debugging purpose i fire select query and get result in dataset and its show data too.but when go into database my inserted data not showing there.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con_str As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
        Dim con As SqlConnection = New SqlConnection(con_str)
        Dim cmd, cmd2 As SqlCommand
        Dim adp As New SqlDataAdapter
        Dim ds As New DataSet

        With adp
            .SelectCommand = New SqlCommand
            With .SelectCommand
                .CommandType = CommandType.StoredProcedure
                .CommandText = "test_insert"
                If con.State = ConnectionState.Closed Then
                    con.Open()

                End If
                .Parameters.Add("cust_name", SqlDbType.VarChar).Value = "hello"
                .Connection = con
            End With
            .Fill(ds, "insert")
        End With


        With adp
            .SelectCommand = New SqlCommand
            With .SelectCommand
                .CommandType = CommandType.StoredProcedure
                .CommandText = "test_select"
                If con.State = ConnectionState.Closed Then
                    con.Open()

                End If
                .Connection = con
            End With
            .Fill(ds, "select")
        End With





    End Sub
End Class


Table test

cust_id	numeric(18, 0)
cust_name	varchar(50)



My Store Procedures


SQL
ALTER PROCEDURE test_insert
    @cust_name varchar(50)
AS
    insert into test(cust_name) values (@cust_name)
    RETURN




SQL
ALTER PROCEDURE test_select
AS
    select * from test
    RETURN
Posted
Updated 30-Mar-10 22:59pm
v2

This might not be the problem but I noticed your connection string points to a database file called Database1.mdf instead of a database on a server.

Might the problem be that you're looking in a database for the data but the application inserted it in the database file?
 
Share this answer
 
well u might be a right.because yesterday i tried to put hard path/direct path and it works fine.in fact i got my old data too.i gave path of "bin/debug".so i check option of "Copy To Output Directory" but i selected already "Copy always" option.but still problem not solved.
 
Share this answer
 
once u have defined the parameters of the insert statement in ur project add this small line
cmd.ExecuteNonQuery()
and u can see the data in the database

Try this out and see whether it works or not

i have tried the same in ASP.NET this is a dummy insert statement and works just fine i hope it helps u my friend

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static string getconnection()
{
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Registration form\ASP.NET.mdb;Persist Security Info=False";
}
protected void Button1_Click(object sender, EventArgs e)
{

OleDbConnection con = new OleDbConnection();
con.ConnectionString = getconnection();
con.Open();
string command = "insert into mytable values(@maiid,@name,@password)";
OleDbCommand cmd = new OleDbCommand(command, con);
cmd.Parameters.AddWithValue("@mailid", txtmailid.Text);
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@password", txtpassword.Text);
int x= cmd.ExecuteNonQuery();
if (x != 0)
{
// Redirecting to some other page u can use a Messagebox
Response.Redirect("Login.aspx");
}

con.Close();
}
}

This is a C# code if u look at it correctly u will be able to convert it into VB.NET code as only the syntax differs the remaining part is same..


Do Rate my answer once u find it useful

Thanks & Regards
Radix :rose:
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900