HI

Friends i want to do the connective in C# window application with sql server and in that even i want that if client put information in text box's and click on submit the information should go in sql server db and then again if he is putting same information in text box he should ref in that please help i have tried a lot but it is not happening please its urgent if any one can help me with the code please

Yoh bra, you need to slow down. I wanna help you but your question is not making much sense. explain to us what exactly what are you trying to do. recommended put a code spinet here.

Before you connect to a Database you need an existing Database file / server. if that exists, then you need a connection to your database. which specifies the database type, location, Login details and maybe some more information depending on your DB provider.

So whats your Database ? Mysql? Access? SQL SERVER 2008 ?

Thanks for you reply but i have help by self :)

On Page load event write this code

private void page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Server=NGENIOUS-WSSDEV\SQLEXPRESS;Database=MIC;Trusted_Connection= True");
// SqlCommand cmd = new SqlCommand("select * from measurementunit",conn);
SqlDataAdapter da = new SqlDataAdapter("select * from measurementunit",conn);
conn.Open();
DataSet ds = new DataSet();

da.Fill(ds, "measurementunit");
conn.Close();
}

then on click of button

private void btninsert_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(@"Server=NGENIOUS-WSSDEV\SQLEXPRESS;Database=MIC;Trusted_Connection= True");

// cnn.ConnectionString = ;
cnn.Open();

// cmd.Connection = cnn;
//cmd.CommandType = CommandType.Text;

SqlParameter p1 = new SqlParameter("@measurment", txtmu.Text);
SqlParameter p2 = new SqlParameter("@mucode", txtmucode.Text);
string query = "insert into measurementunit values('" + p1.Value + "','" + p2.Value + "')";
SqlCommand cmd = new SqlCommand(query,cnn);
// cmd.CommandText = "insert into measurementunit('"+ p1.Value+"','"+p2.Value+"')";
//cmd.CommandType = CommandType.Text;
//SqlDataReader dr = cmd.ExecuteReader();

cmd.ExecuteNonQuery();
cnn.Close();


MessageBox.Show("Your information have Submited");
}

First code will help you to do connection of code and button click will help you to insert data in sql server db Laugh Thumbs Up Rose

CURD(create,update,select(read),delete) sql server 1 post tutorial it will must help you

dear this is basic examples for check user and pass from sql server database and also has CURD

select:

        String Password=null;
        String query_string = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\myDatabase.mdf"";Integrated Security=True;User Instance=True;";

        SqlConnection conn = new SqlConnection(query_string);

                conn.Open();

       string sql="select Password from Users where Email='"+email_textbox.Text+"'";
       //status_Label.Text= sql;

        SqlCommand sql_com = new SqlCommand(sql,conn);
             //Password = cmd.ExecuteScalar() as String;  OR

        SqlDataReader sql_rea = sql_com.ExecuteReader();
        while (sql_rea.Read())
        {
            Password = sql_rea["Password"].ToString();
        }

        if (Password == password_textbox.Text)
        {
           status_Label.Text = "You R Login";
            //Response.Redirect("Main_Menu.aspx");
        }
        else
        {
            status_Label.Text = "Sorry!";
        }
        conn.Close();

insert: (insert user infomation)

        String name =TextBox1.Text;
        String email = TextBox2.Text;
        String pass = TextBox3.Text;
        String query_string = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\myDatabase.mdf"";Integrated Security=True;User Instance=True;";
        SqlConnection conn = new SqlConnection(query_string);

        conn.Open();

        string sql = "insert into Users(Name,Email,Password) values('"+name+"','"+email+"','"+pass+"')";


        SqlCommand sql_com = new SqlCommand(sql, conn);


        int result = sql_com.ExecuteNonQuery();

        if (result==1)
        {
            status_Label0.Text = "1 Row sucessfully Inserted!";
            //Response.Redirect("Main_Menu.aspx");
        }
        else
        {
            status_Label0.Text = "Sorry Row Not Inserted!";
        }
        conn.Close();

update:(update user information)

        String email = TextBox4.Text;

        String query_string = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\myDatabase.mdf"";Integrated Security=True;User Instance=True;";
        SqlConnection conn = new SqlConnection(query_string);

        conn.Open();

        string sql = "update Users set Email='xyz@yahoo.com' where Email='"+email+"'";


        SqlCommand sql_com = new SqlCommand(sql, conn);


        int result = sql_com.ExecuteNonQuery();

        if (result == 1)
        {
            status_Label1.Text = "1 Row sucessfully updated!";
            //Response.Redirect("Main_Menu.aspx");
        }
        else
        {
            status_Label1.Text = "Sorry Row Not updated!";
        }
        conn.Close();

delete:(delete user information)

        String name = TextBox6.Text;

        String query_string = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\myDatabase.mdf"";Integrated Security=True;User Instance=True;";
        SqlConnection conn = new SqlConnection(query_string);

        conn.Open();

        string sql = "delete from Users where Name='" + name + "'";


        SqlCommand sql_com = new SqlCommand(sql, conn);


        int result = sql_com.ExecuteNonQuery();

        if (result == 1)
        {
            status_Label2.Text = "1 Row sucessfully Deleted!";
            //Response.Redirect("Main_Menu.aspx");
        }
        else
        {
            status_Label2.Text = "Sorry Row Not Deleted!";
        }
        conn.Close();

Thanks Dear If anyone still problem then must reply
I Love Islam by ahsan

CURD(create,update,select(read),delete) sql server 1 post tutorial it will must help you

dear this is basic examples for check user and pass from sql server database and also has CURD

select:

        String Password=null;
        String query_string = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\myDatabase.mdf"";Integrated Security=True;User Instance=True;";

        SqlConnection conn = new SqlConnection(query_string);

                conn.Open();

       string sql="select Password from Users where Email='"+email_textbox.Text+"'";
       //status_Label.Text= sql;

        SqlCommand sql_com = new SqlCommand(sql,conn);
             //Password = cmd.ExecuteScalar() as String;  OR

        SqlDataReader sql_rea = sql_com.ExecuteReader();
        while (sql_rea.Read())
        {
            Password = sql_rea["Password"].ToString();
        }

        if (Password == password_textbox.Text)
        {
           status_Label.Text = "You R Login";
            //Response.Redirect("Main_Menu.aspx");
        }
        else
        {
            status_Label.Text = "Sorry!";
        }
        conn.Close();

insert:(insert user infomation)

        String name =TextBox1.Text;
        String email = TextBox2.Text;
        String pass = TextBox3.Text;
        String query_string = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\myDatabase.mdf"";Integrated Security=True;User Instance=True;";
        SqlConnection conn = new SqlConnection(query_string);

        conn.Open();

        string sql = "insert into Users(Name,Email,Password) values('"+name+"','"+email+"','"+pass+"')";


        SqlCommand sql_com = new SqlCommand(sql, conn);


        int result = sql_com.ExecuteNonQuery();

        if (result==1)
        {
            status_Label0.Text = "1 Row sucessfully Inserted!";
            //Response.Redirect("Main_Menu.aspx");
        }
        else
        {
            status_Label0.Text = "Sorry Row Not Inserted!";
        }
        conn.Close();

update:(update user information)

        String email = TextBox4.Text;

        String query_string = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\myDatabase.mdf"";Integrated Security=True;User Instance=True;";
        SqlConnection conn = new SqlConnection(query_string);

        conn.Open();

        string sql = "update Users set Email='xyz@yahoo.com' where Email='"+email+"'";


        SqlCommand sql_com = new SqlCommand(sql, conn);


        int result = sql_com.ExecuteNonQuery();

        if (result == 1)
        {
            status_Label1.Text = "1 Row sucessfully updated!";
            //Response.Redirect("Main_Menu.aspx");
        }
        else
        {
            status_Label1.Text = "Sorry Row Not updated!";
        }
        conn.Close();

delete:(delete user information)

        String name = TextBox6.Text;

        String query_string = @"Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\myDatabase.mdf"";Integrated Security=True;User Instance=True;";
        SqlConnection conn = new SqlConnection(query_string);

        conn.Open();

        string sql = "delete from Users where Name='" + name + "'";


        SqlCommand sql_com = new SqlCommand(sql, conn);


        int result = sql_com.ExecuteNonQuery();

        if (result == 1)
        {
            status_Label2.Text = "1 Row sucessfully Deleted!";
            //Response.Redirect("Main_Menu.aspx");
        }
        else
        {
            status_Label2.Text = "Sorry Row Not Deleted!";
        }
        conn.Close();

Thanks Dear If anyone still problem then must reply
I Love Islam by ahsan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.