PDA

View Full Version : سوال: ثبت اطلاعات در بانک اطلاعاتی+فرم c#+gridview



mnasrin
جمعه 29 آبان 1388, 10:46 صبح
من یه برنامه نوشتم که ارتباط بsql, c#2008است .موقعی که در فرم اطلاعات را وارد می کنم همون لحظه در datagridview اطلاعات را نمایش میده ولی موقعی که آن فرم را می بندم وباز می کنم آن اطلاعات نیست در واقع در بانک ثبت نمیشه .دوستان به نظر شما چیکار کنم ؟
لطفا فوری فوری کمکم کنید.

AliRezaPro
جمعه 29 آبان 1388, 12:08 عصر
شما تکه کد تون را اینجا بزارید

mnasrin
جمعه 29 آبان 1388, 12:21 عصر
از این راه رفتم اعمال نکرد روی بانکم

public void Fill()

{
SqlConnection ObjConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\anjoman.mdf;Integrated (file://\\anjoman.mdf;Integrated) Security=True;Connect Timeout=30;User Instance=True");
SqlDataAdapter ObjDataAdapter = new SqlDataAdapter("Select * From mashin", ObjConnection);
DataSet ObjDataSet = new DataSet();
ObjConnection.Open();
ObjDataAdapter.Fill(ObjDataSet, "mashin");
ObjConnection.Close();
dataGridView2.AutoGenerateColumns = true;
dataGridView2.DataSource = ObjDataSet;
dataGridView2.DataMember = "mashin";
ObjDataAdapter = null;
ObjConnection = null;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection ObjConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\anjoman.mdf;Integrated (file://\\anjoman.mdf;Integrated) Security=True;Connect Timeout=30;User Instance=True");
SqlCommand ObjCommand = new SqlCommand("Update mashin Set fname=@fname,lname=@lname,nhushmandmashin=@nhushma ndmashin,npelak=@npelak,noemashin=@noemashin,moaye nefani=@moayenefani,takhalofm=@takhalofm,molahezat =@molahezat,adress=@adress,tel=@tel,mobile=@mobile Where idm=@idm;", ObjConnection);
ObjCommand.Parameters.AddWithValue("@fname", txt_fname.Text);
ObjCommand.Parameters.AddWithValue("@lname", txt_lname.Text);
ObjCommand.Parameters.AddWithValue("@idm", txt_ksm.Text);
ObjCommand.Parameters.AddWithValue("@nhushmandmashin", txt_nk.Text);
ObjCommand.Parameters.AddWithValue("@npelak", "ایران " + txt_pelak1.Text + cmbp.SelectedItem.ToString() + txt_pelak2.Text + txt_pelak3.Text);
ObjCommand.Parameters.AddWithValue("@noemashin", cmb_nm.SelectedItem.ToString());
ObjCommand.Parameters.AddWithValue("@moayenefani", cmb_fani.SelectedItem.ToString());
ObjCommand.Parameters.AddWithValue("@takhalofm", cmb_t.SelectedItem.ToString());
ObjCommand.Parameters.AddWithValue("@molahezat", textBox1.Text);
ObjCommand.Parameters.AddWithValue("@adress", txt_address.Text);
ObjCommand.Parameters.AddWithValue("@tel", txt_tell.Text);
ObjCommand.Parameters.AddWithValue("@mobile", txt_mobile3.Text);
ObjConnection.Open();
ObjCommand.ExecuteNonQuery();
try { ObjCommand.ExecuteNonQuery(); }
catch (Exception exc) { MessageBox.Show(exc.Message); }
ObjConnection.Close();
Fill();

}

mnasrin
جمعه 29 آبان 1388, 12:23 عصر
از این راهم رفتم بار اعمال نکرد

private void Add_Click(object sender, EventArgs e)
{
mashinTableAdapter.Insert(txt_fname.Text, txt_lname.Text, txt_nk.Text, "ایران " + txt_pelak1.Text + cmbp.SelectedItem.ToString() + txt_pelak2.Text + txt_pelak3.Text, cmb_nm.SelectedItem.ToString(), cmb_fani.Text, cmb_t.SelectedItem.ToString(), textBox1.Text, txt_address.Text, txt_tell.Text, txt_mobile3.Text);
mashinTableAdapter.Fill(anjomanDataSet.mashin);


}

mnasrin
جمعه 29 آبان 1388, 12:29 عصر
هرچه زودتر کمکم کنید زمان ندارم

sky_in_iran
جمعه 29 آبان 1388, 12:43 عصر
سلام دوست عزيز لطف كن برناتو بزار يا اينكه كدتو تو تگ code بزار كمي اينجوري نا خواناست كمك كردن سخته اما من يك كد نمونه ميزارم با كد خودت چك كن انشا الله كارت را بيفته :




public bool EditProduct(string ProductID ,string StoryID,string StoryName,string ProductCode,string type,string Category,string Cost,string Country,string Detail,string Colors,byte[] Picture,int VotingGood,int VotingBad,int VotingUltra)
{
try
{
cnn = new SqlConnection(connect);
cnn.Open();
command = new SqlCommand();
command.Connection = cnn;
command.CommandText = " UPDATE [PRODUCT] SET [StoryID] = @StoryID, [StoryName] = @StoryName, [ProductCode] = @ProductCode, [Type] = @Type, [Category] = @Category, [Cost] = @Cost, [Country] = @Country, [Detail] = @Detail, [Colors] = @Colors, [Picture] = @Picture, [VotingGood] = @VotingGood, [VotingBad] = @VotingBad, [VotingUltra] = @VotingUltra Where [ProductID] = @ProductID";
command.CommandType = CommandType.Text;
command.Parameters.Add(new SqlParameter("@ProductID", SqlDbType.UniqueIdentifier)).Value = new SqlGuid(ProductID);
command.Parameters.Add(new SqlParameter("@StoryID", SqlDbType.UniqueIdentifier)).Value =new SqlGuid(StoryID);
command.Parameters.Add(new SqlParameter("@StoryName", SqlDbType.NVarChar)).Value = StoryName;
command.Parameters.Add(new SqlParameter("@ProductCode", SqlDbType.NVarChar)).Value = ProductCode;
command.Parameters.Add(new SqlParameter("@Type", SqlDbType.NVarChar)).Value = type;
command.Parameters.Add(new SqlParameter("@Category", SqlDbType.NVarChar)).Value = Category;
command.Parameters.Add(new SqlParameter("@Cost", SqlDbType.NVarChar)).Value = Cost;
command.Parameters.Add(new SqlParameter("@Country", SqlDbType.NVarChar)).Value = Country;
command.Parameters.Add(new SqlParameter("@Detail", SqlDbType.NVarChar)).Value = Detail;
command.Parameters.Add(new SqlParameter("@Colors", SqlDbType.NVarChar)).Value = Colors;
command.Parameters.Add(new SqlParameter("@Picture", SqlDbType.Image)).Value = Picture;
command.Parameters.Add(new SqlParameter("@VotingGood", SqlDbType.Int)).Value = VotingGood;
command.Parameters.Add(new SqlParameter("@VotingBad", SqlDbType.Int)).Value = VotingBad;
command.Parameters.Add(new SqlParameter("@VotingUltra", SqlDbType.Int)).Value = VotingUltra;
int i = command.ExecuteNonQuery();
command.Connection.Close();
command.Dispose();
if (i > 0)
return true;
else
return false;
}
catch { return false; }
}

mnasrin
جمعه 29 آبان 1388, 16:26 عصر
دقیقا برنامه منم همینطوریه ولی در بانک ثبت نمیشه؟؟؟؟

sky_in_iran
جمعه 29 آبان 1388, 18:53 عصر
دوست عزيز برنامت و تريس كن ببين connection درست كار ميكنه شايد از اون باشه به جاي "0"تو data source اسمي كه sql داره رو بزار Data Source=savalan\\SQLEXPRESS